How to get ICredential of running user in asp.net
How do I get an ICredential object within my web application?
- I use Windows Authentication
I tried using the following code:
WindowsImpersonationContext securityContext =
开发者_C百科 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"
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 :-)
Where do you need it for?
You can retrieve the username from System.Threading.Thread.CurrentPrincipal.Identity
精彩评论