2011-06-05 82 views
1

我正在使用基本授权编写RESTful服务。wcf 401并要求在RESTful服务中登录/密码

这是我做的时候没有授权头存在,或者当有错误UN /密码

//Get authorization header 
      var auth = HttpContext.Current.Request.Headers.GetValues("Authorization"); 
      if (auth == null) 
      { 
       outgoingResponse.StatusCode = HttpStatusCode.Unauthorized; 
       return false; 
      } 

      //Parse auth header: 
      var authString = auth[0]; 
      String loginName, password; 
      try 
      { 
       var decbuff = Convert.FromBase64String(authString.Replace("Basic ", "")); 
       loginName = System.Text.Encoding.UTF8.GetString(decbuff).Split(':')[0]; 
       password = System.Text.Encoding.UTF8.GetString(decbuff).Split(':')[1]; 
      } 
      catch 
      { 
       outgoingResponse.StatusCode = HttpStatusCode.Unauthorized; 
       outgoingResponse.StatusDescription = "Invalid Authorization header"; 
       return false; 
      } 

当我看着菲德勒我看到这一点:

enter image description here

它的工作原理确定与我的客户端(Android),但我希望这项服务可以通过浏览器或其他浏览器浏览。如果我发送401,我如何让资源管理器询问联合国/密码?我需要指定一些东西吗?

谢谢

回答

0

我想通了。我需要把HTTP标头401这样一起:

WWW身份验证:基本境界=“安全 区”

然后浏览器知道并显示登录窗口用户