views:

89

answers:

2

How do I get an ICredential object within my web application?

  • I use Windows Authentication

I tried using the following code:

WindowsImpersonationContext securityContext = 
      Request.LogonUserIdentity.Impersonate(); 

After the last line of code both: CredentialCache.DefaultCredentials and CredentialCache.DefaultNetworkCredentials are still empty.

I know that the identity is right because the following property shows the right domain\user account I'm using: Request.LogonUserIdentity.Name => "domain\user"

This is the authentication type of that object:

Request.LogonUserIdentity.AuthenticationType => "NTLM"

A: 

Where do you need it for?

You can retrieve the username from System.Threading.Thread.CurrentPrincipal.Identity

rdkleine
+1  A: 

In order to get an ICredential implementation of the user, you first need to get a hold of the WindowsIdentity for the user (e.g. through Page.User.Identity). With the WindowsIdentity object you can impersonate the user. After successful impersonation you can get a hold of the credentials for the user through CredentialCache.DefaultCredentials.

The question now is, what do you want to do with the credentials? If you want to use those credentials to access some ressource on a different server you'd probably have to have Keberos delegation enabled to allow the server hop. I wouldn't know how to do that :-)

Jakob Christensen
I tried this approach, but I still have the CredentialCache empty (I've updated the post with the code I used)
tivo
Did you put <identity impersonate="true" /> in web.config? I don't know if it matters. Maybe it not possible to impersonate in this way for in an ASP.NET application.
Jakob Christensen