2

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

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;
4

1 回答 1

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
于 2018-12-13T13:43:42.913 回答