简体   繁体   中英

Get Default Credentials From A Windows Service

I want to be able to get the default credentials from a within a windows service. However if I call

var credentials = CredentialCache.DefaultCredentials;

Or

var networkCredentials = CredentialCache.DefaultNetWorkCredentials;

The NetworkCredential returned (as an ICredential) the UserName and Domain etc are empty. I am running the windows service under a domain account.

If I create a simple console application then a valid credential is returned.

The reason I need this is to be able to set the credentials on a signalR HubConnection. The SignalR service is running in IIS with Windows Authentication enabled. The only way I can see to do this is to set the HubConnection.Credentials property. But as explained above I cannot get the credentials when I am running in a windows service.

Can anyone help?

I found this was not the issue after all and not being able to retrieve the credentials was a red herring (I found I could not retrieve them in the console either), when calling into the SignalR hub the identity is passed and the request authenticated by IIS.

But to conclude. This was my stupid mistake, nothing to do with the default credentials.

As a side note, the credentials are not available to retrieve on purpose see link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM