读取HTTP认证中的Realm属性

发布于 2024-12-20 01:50:20 字数 64 浏览 2 评论 0原文

如何读取请求 HTTP 基本身份验证的服务器在 WWW-Authenticate 标头中发送的 Realm 属性?

How can I read the Realm property sent in the WWW-Authenticate header by a server requesting HTTP Basic authentication?

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

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

发布评论

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

评论(2

过期以后 2024-12-27 01:50:20

不太确定反对者对这个问题的真正问题是什么。

以下是获取包含基本身份验证领域的 WWW-Authenticate 标头的粗略代码。从标头中提取实际领域值作为练习,但应该非常简单(例如使用正则表达式)。

public static string GetRealm(string url)
{
    var request = (HttpWebRequest)WebRequest.Create(url);
    try
    {
        using (request.GetResponse())
        {
            return null;
        }
    }
    catch (WebException e)
    {
        if (e.Response == null) return null;
        var auth = e.Response.Headers[HttpResponseHeader.WwwAuthenticate];
        if (auth == null) return null;
        // Example auth value:
        // Basic realm="Some realm"
        return ...Extract the value of "realm" here (with a regex perhaps)...
    }
}

Not really sure what the issue the down-voters have with this question really is.

Here's the rough code to get the WWW-Authenticate header that contains the Basic authentication realm. Extracting the actual realm value from the header is left as an exercise, but should be quite straightforward (e.g. using regular expression).

public static string GetRealm(string url)
{
    var request = (HttpWebRequest)WebRequest.Create(url);
    try
    {
        using (request.GetResponse())
        {
            return null;
        }
    }
    catch (WebException e)
    {
        if (e.Response == null) return null;
        var auth = e.Response.Headers[HttpResponseHeader.WwwAuthenticate];
        if (auth == null) return null;
        // Example auth value:
        // Basic realm="Some realm"
        return ...Extract the value of "realm" here (with a regex perhaps)...
    }
}
感情旳空白 2024-12-27 01:50:20

我假设您想要创建带有基本身份验证的 Web 请求。

如果这是正确的假设,那么您需要以下代码:

// Create a request to a URL
WebRequest myReq = WebRequest.Create(url);
string usernamePassword = "username:password";
//Use the CredentialCache so we can attach the authentication to the request
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
//Send and receive the response
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();

I'm assuming you want to create a Web Request with Basic Authentication.

If that's the correct assumption, the following code is what you need:

// Create a request to a URL
WebRequest myReq = WebRequest.Create(url);
string usernamePassword = "username:password";
//Use the CredentialCache so we can attach the authentication to the request
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
//Send and receive the response
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文