HTTP-Authentication 将令牌添加到客户端
我可以设法让 API 服务器向我发送一个令牌。我也可以将令牌存储在 CustomModel 中。 但是我如何将令牌附加到我想使用的 HttpClient 上。
这是我的登录名和函数:
public static async Task<AuthToken> Login()
{
var userName = "administrator";
var passwd = "********";
var ClientID = "acef4bf9988848a0bff79485d1ac2648";
var ClientScrt = "********************";
var url = "https://****.********.****/MobiControl/api/token";
string body = $"grant_type=password&username={userName}&password={passwd}"; //"grant_type=password&username=Administrator&password=1";
var payload = new StringContent(body, Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
var authHeader = Encoding.ASCII.GetBytes($"{ClientID}:{ClientScrt}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeader));
var result = client.PostAsync(url, payload);
var token = await result.Result.Content.ReadAsStringAsync();
AuthToken authToken = JsonConvert.DeserializeObject<AuthToken>(token);
Console.WriteLine(token);
ApiHelper.InitializeClient();
return authToken;
}
}
这是我的客户端构造函数:
注释行产生了问题,它给了我两个错误:“非静态字段、方法或属性‘成员’需要对象引用”
public static class ApiHelper
{
public static HttpClient ApiClient { get; set; }
public static void InitializeClient()
{
ApiClient = new HttpClient();
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//ApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthToken.Token_type, AuthToken.Access_token);
}
}
I could manage to get the API-Server sending me a Token. Also i can store the Token in a CustomModel.
But How can i attach the Token to my HttpClient which i want to use.
Here's my Login and function:
public static async Task<AuthToken> Login()
{
var userName = "administrator";
var passwd = "********";
var ClientID = "acef4bf9988848a0bff79485d1ac2648";
var ClientScrt = "********************";
var url = "https://****.********.****/MobiControl/api/token";
string body = quot;grant_type=password&username={userName}&password={passwd}"; //"grant_type=password&username=Administrator&password=1";
var payload = new StringContent(body, Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
var authHeader = Encoding.ASCII.GetBytes(quot;{ClientID}:{ClientScrt}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeader));
var result = client.PostAsync(url, payload);
var token = await result.Result.Content.ReadAsStringAsync();
AuthToken authToken = JsonConvert.DeserializeObject<AuthToken>(token);
Console.WriteLine(token);
ApiHelper.InitializeClient();
return authToken;
}
}
and here's my Client constructor:
the commented line makes the problem(s), it gives me two faults: "An object reference is required for the nonstatic field, method, or property 'member'"
public static class ApiHelper
{
public static HttpClient ApiClient { get; set; }
public static void InitializeClient()
{
ApiClient = new HttpClient();
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//ApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthToken.Token_type, AuthToken.Access_token);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论