Why does CredentialCache.DefaultCredential contain

2019-02-25 05:42发布

Does anyone have any ideas as to why CredentialCache.DefaultCredential would return an ICredential instance with empty strings for domain, username, and password? I'm running a WCF service on IIS 7.5. It works fine on one server but never works on another. I have verified that the IIS application has Windows Authentication enabled....

Here is how it's being used:

string url = string.Format("{0}/departments/finance/_vti_bin/listdata.svc", _IntranetAddress);
var financeDataContext = new FinanceDataContext(new Uri(url))
{
    Credentials = CredentialCache.DefaultCredentials
};

2条回答
Melony?
2楼-- · 2019-02-25 06:11

I am not sure how it is working in one of your servers? I hope you already read this http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultcredentials.aspx but it clearly says "The ICredentials instance returned by DefaultCredentials cannot be used to view the user name, password, or domain of the current security context."

查看更多
Lonely孤独者°
3楼-- · 2019-02-25 06:20

The NetworkCredential returned from CredentialCache.DefaultCredential is just a placeholder. If you look at it using the Debugger, you'll see that it's of type SystemNetworkCredential. Internal API check for this type to see if integrated authentication should be used or not. There are other ways to get the current username (like WindowsIdentity.GetCurrent()).

EDIT: To specify impersonation for a WCF operation, add this attribute to the method implementing a contract:

[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public void SomeMethod() 
{
    // do something here
}
查看更多
登录 后发表回答