<分区>
如何读取请求 HTTP 基本身份验证的服务器在 WWW-Authenticate header 中发送的 Realm 属性?
<分区>
如何读取请求 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/