将 HTTP 身份验证与 C# WebRequest 结合使用

发布于 2024-07-16 09:24:56 字数 76 浏览 11 评论 0原文

我想向需要身份验证的页面发出网络请求。 我该怎么做呢? 我发现一些内容可能会使用 Credentials 属性,但我不确定如何使用它。

I want to make a web request to a page that needs authenticating. How would I go about doing this? I found something that said possibly to use the Credentials property, but I'm not sure how to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-07-23 09:24:56

将新的 NetworkCredential 实例分配给 Credentials 属性:

webClient.Credentials = new NetworkCredential("Mehrdad", "Password");

Assign a new NetworkCredential instance to the Credentials property:

webClient.Credentials = new NetworkCredential("Mehrdad", "Password");
甜警司 2024-07-23 09:24:56

基本身份验证示例:

public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
    string authInfo = userName + ":" + userPassword;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
}

http://blog.kowalczyk.info/article/ at3/Forcing-basic-http-authentication-for-HttpWebReq.html

Basic auth example:

public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
    string authInfo = userName + ":" + userPassword;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
}

http://blog.kowalczyk.info/article/at3/Forcing-basic-http-authentication-for-HttpWebReq.html

死开点丶别碍眼 2024-07-23 09:24:56

也可以自动进行身份验证。 这将使用当前登录用户的凭据。

webClient.Credentials = CredentialCache.DefaultCredentials

It is also possible to authenticate automatically with. This will use the credentials of the currently logged on user.

webClient.Credentials = CredentialCache.DefaultCredentials
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文