簡體   English   中英

HttpListener摘要身份驗證架構

[英]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
};

http://msdn.microsoft.com/en-us/library/system.net.httplistener.authenticationschemeselectordelegate.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM