2011-03-07 117 views
2

我必须实现一个小的REST服务器来管理远程数据库,没什么特别的。 安全性不是关键问题,因为此服务器必须在Intranet环境中运行;我们只想过滤用户并将其重定向到合适的资源。HttpListener摘要身份验证架构

 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() returns-我可以从用户对象检查用户名和密码,并确定如何处理该请求。 更改初始listener.AuthenticationSchemes = AuthenticationSchemes.Basic

listener.AuthenticationSchemes = AuthenticationSchemes.Digest 

它停止工作,我期待和基本身份验证模式有效地做到。 listener.GetContext()调用永不返回。 HttpListener SEEMS阻止任何请求,并从客户端,我继续被提示输入用户名和密码。 我已经尝试了本地用户,本地管理员,域用户,域管理员,约500个幻想名称:没有任何工作。 GetContext()不再返回。 你能帮我吗?

在此先感谢。

L.

回答

0

您可以使用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

0

分配给listener.Realm的值必须被用于认证的Windows域的名称。 “testserver1”看起来不像我的域名。