[英]HttpListener with JWT and Basic auth: how to send WWW-Authenticate? (Self-Hosted)
[英]HttpListener Digest Auth schema
我必须实现一个小型REST服务器来管理远程数据库,没什么特别的。 安全性不是关键问题,因为该服务器必须在Intranet环境中运行。 我们只想过滤用户并将其重定向到适当的资源。
HttpListener listener = new HttpListener();
listener.Realm = "testserver1";
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
string responseString = "<HTML><BODY>" + DateTime.Now.ToString() + "</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
listener.Stop();
此代码(从Microsoft网站获取)在服务器端和-当listener.GetContext()返回时都可以正常使用-我可以从User对象检查用户名和密码,并确定如何处理请求。 更改初始listener.AuthenticationSchemes = AuthenticationSchemes.Basic为
listener.AuthenticationSchemes = AuthenticationSchemes.Digest
它按照我的期望和基本身份验证架构有效地停止工作。 listener.GetContext()调用永不返回。 HttpListener SEEMS阻止任何请求,并且从客户端继续提示我输入用户名和密码。 我尝试使用本地用户,本地管理员,域用户,域管理员,大约500个幻想名称:没有任何效果。 GetContext()不再返回。 你能帮助我吗?
提前致谢。
L.
分配给listener.Realm
的值必须是用于身份验证的Windows域的名称。 “ testserver1”在我看来并不像域名。
您可以使用对我有用的AuthenticationSchemeSelectorDelegate。 例:
_listener.AuthenticationSchemeSelectorDelegate = delegate(HttpListenerRequest request)
{
string temp = request.Headers["Authorization"];
if (!string.IsNullOrEmpty(temp))
throw new Exception("Auth string: " + temp);
return AuthenticationSchemes.Digest; // here where you return auth type for every request eg. can be Basic,Digest
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.