c# httpwebrequest登录集成window身份验证的网站,有用户名和密码

一只鬼 2020-05-13 04:21:40
这是fiddler获取到的response

HTTP/1.1 401 Unauthorized
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 16:00:00 GMT; path=/; HttpOnly
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-Content-Type-Options: nosniff
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
P3P: CP="NON"
X-XSS-Protection: 0
Content-Length: 1181
Proxy-Support: Session-Based-Authentication
下面是我的代码


ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("url");

req.PreAuthenticate = true;
req.UseDefaultCredentials = false;
var c = new NetworkCredential("user", "password");
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("url"), "Negotiate", c);

req.KeepAlive = true;

req.Credentials = credentialCache;

req.Method = "GET";
req.Host = "";
//req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req.Headers.Add("Upgrade-Insecure-Requests", "1");
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36";
req.Headers.Add("Cache-Control", "max-age=0");
req.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
req.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");

HttpWebResponse res = null;


try
{
res = (HttpWebResponse)req.GetResponse();
}
catch (WebException er)
{
res = (HttpWebResponse)er.Response;
}


返回结果一直是401,为什么?浏览器输入账号和密码就能登录进去了,网站是https的
...全文
576 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
一只鬼 2020-05-14
  • 打赏
  • 举报
回复
需要把密码加密吗

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧