2012-04-28 55 views
0

我有一個手機應用程序,使用改掉RestSharp如何閱讀在ASP.Net MVC4 API GET請求

 private void ButtonTestTap(object sender, EventArgs e) 
    { 
     var client = new RestClient 
     { 
      CookieContainer = new CookieContainer(), 
      BaseUrl = "http://localhost:21688/api/game", 
      Authenticator = new HttpBasicAuthenticator("muhcow", "123456") 
     }; 

     RestRequest request = new RestRequest(Method.GET); 
     request.AddParameter("id", 5); 

     //request.AddBody(5); 
     client.GetAsync<LoginResult>(request, (response, ds) => 
     { 
      System.Diagnostics.Debug.WriteLine(response.StatusDescription); 
      System.Diagnostics.Debug.WriteLine(response.Data); 
      System.Diagnostics.Debug.WriteLine(response.Content); 
     }); 
    } 

要想從我的網頁API數據,然後想讀取身份驗證的基本授權=新的HttpBasicAuthenticator(「muhcow」,「123456」),當我的api服務器收到這個GET請求時,我可以驗證用戶,但我不知道如何讀取數據。

我有這個

public class GameController : ApiController 
    { 
     // GET /api/game/5 
    public string Get(int id) 
    { 
     var sdf2 = ControllerContext.Request.Headers.Authorization.Parameter; 

     //return LoginManager.VerifyLogin(loginData); 
     return "Some data"; 
    } 

但SDF2只是有一個奇怪的值 「bXVoY293OjEyMzQ1Ng ==」

回答

2

這頭是base64編碼。將Convert.FromBase64String()應用於它,你會看到內容。

1
System.Text.Encoding.UTF8.GetString (Convert.FromBase64String (this.ControllerContext 

.Request .Headers.Authorization.Parameter)) 

結果是 「muhcow」,「123456」