网络凭据未保存在CredentialCache中

时间:2012-09-13 12:43:05

标签: c# asp.net .net networkcredentials

我必须将凭证缓存中的用户名和密码分配给Httpwebrequest。当我分配给缓存时,它正在工作,但是当从缓存分配给Webrequest时,它显示为null。哪里我错了?

string strServer = "";
        strServer = "http://servername/";
        Uri uri = new Uri(strServer);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        CredentialCache cache = new CredentialCache();  
        cache.Add(new Uri(strServer), "Basic", new System.Net.NetworkCredential("username", "password","Domain"));
        cache.Add(new Uri(strServer), "Digest", new System.Net.NetworkCredential("username", "password","Domain"));
        request.Credentials = cache;

1 个答案:

答案 0 :(得分:0)


你好, 您只需要从高速缓存中检索凭证,然后将其添加到Web请求中,例如以下所示:

    cache.Add(new Uri(strServer), "Basic", new System.Net.NetworkCredential("username", "password","Domain"));
    cache.Add(new Uri(strServer), "Digest", new System.Net.NetworkCredential("username", "password","Domain"));    

    NetworkCredential  currentCredentials= cache.GetCredential(new Uri(strServer), "Basic");  //Get specific credential
    request.Credentials = currentCredentials; //Allocate credential