简体   繁体   English

NetworkCredential适用于POST,但不适用于GET

[英]NetworkCredential working for POST but not GET

I'm calling a webAPI web service that uses windows authentication. 我正在调用使用Windows身份验证的webAPI Web服务。 I'm using this code in my development environment since I am developing from a box that is not on the domain. 我在开发环境中使用此代码,因为我是从不在域中的盒子进行开发的。

 var req = (HttpWebRequest)WebRequest.Create(url);
 req.Credentials = new NetworkCredential("username", "password", "domain");
 req.Method = WebRequestMethods.Http.Post; //or get

This works just fine when I do a post, but when I want to do a get it doesn't work. 当我发表文章时,这种方法就很好了,但是当我想要获取它时,它是行不通的。

When i visit the url for the get in a web browser it asks for my username and password, as expected. 当我在Web浏览器中访问get的URL时,它会按预期要求我的用户名和密码。 I enter the username and password correctly and the GET works. 我输入了正确的用户名和密码,GET正常工作。

Try basic authentication as below: 尝试如下进行基本身份验证:

string credentials = String.Format("{0}:{1}", username, password);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
req.Headers.Add("Authorization", authorization);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM