我必须将凭据缓存中的用户名和密码分配给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;
你好您只需要从缓存中检索凭据,然后将其添加到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