2011-06-05 38 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身份驗證:基本境界=「安全 區」

然後瀏覽器知道並顯示登錄窗口用戶