【发布时间】:2018-04-11 20:22:59
【问题描述】:
我正在阅读一篇带有代码示例的文章,该示例展示了如何使用 api 实现基本身份验证。文章链接是https://www.infoworld.com/article/2990800/application-architecture/implement-http-authentication-in-web-api.html
查看他们将凭据发送到服务器的客户端代码。
public void BasicAuthenticationTest()
{
string username = Convert.ToBase64String(Encoding.UTF8.GetBytes("joydip"));
string password = Convert.ToBase64String(Encoding.UTF8.GetBytes("joydip123"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", username + ":" + password);
var result = client.GetAsync(new Uri("http://localhost/IDG/api/default/")).Result;
Assert.IsTrue(result.IsSuccessStatusCode);
}
特别看这行client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", username + ":" + password);
没有基本字存在授权字后....那么它是怎么回事 工作吗?
签名应该类似于 Authorization: Basic anNtaXRoOlBvcGNvcm4=
所以请有人给我一些关于它的知识。
最后一个问题,如果是基本身份验证,我们可以从服务器端返回一个令牌吗? 如果可能的话,分享一些知识如何在基本身份验证的情况下返回令牌。
谢谢
【问题讨论】:
-
该代码在如何使用
AuthenticationHeaderValuegithub.com/dotnet/corefx/blob/master/src/System.Net.Http/src/… 上也具有误导性
标签: c# asp.net-web-api basic-authentication