rhughes
rhughes

Reputation: 9583

Get `ICredentials` from Windows Authentication

I'm using Windows Authentication.

I need to log into a service. It requires authentication using an ICredentials object, but would rather avoid asking for the user to enter their credentials again.

Is there a way to get the ICredentials from the currently logged in user?

The service is the TFS SDK.

Upvotes: 5

Views: 10025

Answers (1)

Jakub Konecki
Jakub Konecki

Reputation: 46008

Controller in MVC has a property User which is IPrincipal.

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.user%28v=vs.108%29.aspx

IPrincipal has an Identity property

http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.identity%28v=vs.110%29.aspx

which is of type IIdentity

http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity%28v=vs.110%29.aspx

If you're using Windows Authentication the actual instance will be of type WindowsIdentity

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity%28v=vs.110%29.aspx

Hopefully it will contain all the credential information you need.

Also, you might want to try:

Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultNetworkCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

Upvotes: 7

Related Questions