C# HttpListener摘要验证模式

C# HttpListener摘要验证模式,c#,httplistener,digest-authentication,C#,Httplistener,Digest Authentication,我必须实现一个小型REST服务器来管理远程数据库,没有什么特别的。 安全性不是一个关键问题,因为此服务器必须在intranet环境中运行;我们只想过滤用户并将其重定向到适当的资源 HttpListener listener = new HttpListener(); listener.Realm = "testserver1"; listener.AuthenticationSchemes = AuthenticationSchemes.Basic

我必须实现一个小型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();
它停止了我所期望的和基本身份验证模式的有效工作。 listener.GetContext()调用永远不会返回。HttpListener似乎会阻止任何请求,从客户端,我会继续被提示输入用户名和密码。 我试过本地用户,本地管理员,域用户,域管理员,大约500个幻想的名字:什么都不管用。 GetContext()不再返回。 你能帮我吗

提前谢谢


L.

您可以使用AuthenticationSchemeSelect或DeleteGate,为我工作。例如:

_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
};

分配给侦听器的值。域必须是用于身份验证的Windows域的名称。“testserver1”在我看来不像一个域名

_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
};