c# - 读取 HTTP 认证中的 Realm 属性

标签 c# httpwebrequest basic-authentication webresponse

<分区>

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

最佳答案

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

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

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)...
    }
}

关于c# - 读取 HTTP 认证中的 Realm 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8436810/

相关文章:

c# - 带发现的 WCF 失败,套接字在 XP 的上下文中无效?

c# - 具有基本身份验证的 GET 请求适用于 Postman,但不适用于 C#

c# - 限制引用实体的 NHibernate 查询

c# - Kinect,警告 : An ImageFrame instance was not Disposed

c# - 被 C# ownerdraw treeview 困扰

c# - WebRequest 不包含 Windows 10 通用应用程序 'GetResponse' 的定义

c# - C#WebClient-查看源问题

asp.net - C# 中的简单服务器和客户端请求/响应

node.js - 通过Postman和httpie访问CRUD路由时身份验证失败

http - <audio>标签上的基本http身份验证?