简体   繁体   English

Web URL 在 C# 401 未经授权

[英]Web URL in C# 401 Unauthorized

I am trying to hit the Web URL from Dev Remote server.我正在尝试从开发远程服务器访问 Web URL。

var myRequet = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)myRequest.GetResponse();

In the response, I got an error在回复中,我得到了一个错误

"The remote server returned an rerror: (401) Unauthorized". “远程服务器返回错误:(401)未经授权”。

When I hit the url in the browser, the url is asking me for username and password.当我在浏览器中点击 url 时,url 要求我输入用户名和密码。 I gave it and I can access the url.我给了它,我可以访问 url。 Do I need to use Default credentials to open the Url from C# code?我需要使用默认凭据从 C# 代码打开 Url 吗?

Thanks谢谢

this is basic Authorization set header "Authorization" with value "Basic [base64({username}:{pasword})]"这是基本授权集 header “授权”,值为“基本 [base64({username}:{pasword})]”

sample:样本:

myRequest.Headers["Authorization"] = "Basic "+Base64Encode("your Username"+":"+"your Password");
//
public static string Base64Encode(string plainText) {
  var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  return System.Convert.ToBase64String(plainTextBytes);
}

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

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