我必须实现一个小型 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
它停止工作,正如我所期望的那样,并且 Basic auth schema 有效地工作。 listener.GetContext() 调用永远不会返回。 HttpListener SEEMS 阻止任何请求,并且从客户端继续提示我输入用户名和密码。 我已经尝试了本地用户、本地管理员、域用户、域管理员,大约 500 个幻想名称:没有任何作用。 GetContext() 不再返回。 你能帮帮我吗?
提前致谢。
L.
最佳答案
分配给 listener.Realm
的值必须是用于身份验证的 Windows 域的名称。 “testserver1”在我看来不像是域名。
关于c# - HttpListener 摘要身份验证架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219293/