Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 439
rhughes Avatar asked Nov 19 '13 11:11

rhughes


1 Answers

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");
like image 154
Jakub Konecki Avatar answered Oct 16 '22 19:10

Jakub Konecki