我有一个手机应用程序试图使用RestSharp从我的web api获取数据
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);
});
}
然后想要读取Authenticator = new 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 ==”
答案 0 :(得分:2)
该标头是base64编码的。将Convert.FromBase64String()应用于它,您将看到内容。
答案 1 :(得分:1)
Authorization.Parameter是base64编码的。您可以在此处查看如何解码http://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/
的示例答案 2 :(得分:1)
System.Text.Encoding.UTF8.GetString (Convert.FromBase64String (this.ControllerContext
.Request .Headers.Authorization.Parameter ))
结果是 “muhcow”,“123456”