【发布时间】:2017-10-27 22:06:57
【问题描述】:
我有一个 web api 服务和一个 web 客户端应用程序来访问 web api。两者都在 azure 活动目录上注册。 但是,当 Web 客户端应用程序尝试访问 Web api 时,我得到了:
ReasonPhrase: 'Unauthorized'
WWW-Authenticate: Bearer error=\"invalid_token\", error_description=\"The signature is invalid
然后我检查了https://jwt.io/ 上的令牌,它确实显示“无效签名”。但是,我不知道这里有什么问题。
这是我检索令牌的方式:
string authority = "https://login.windows.net/tenantid-log-number/oauth2/token";
string clientID = "83adf895-681a-4dd6-9dfb-2a1484dd4188";
string resourceUri = "https://tenant.onmicrosoft.com/webapiservice";
string appKey = "anJxg3N/5dqiHKx+4zwzFB9A6dN5HdqSitdSOpxzVd=";
ClientCredential clientCredential = new ClientCredential(clientID, appKey);
AuthenticationContext ac = new AuthenticationContext(authority);
Task<AuthenticationResult> authResult = ac.AcquireTokenAsync(resourceUri, clientCredential);
return authResult.Result.AccessToken;
以下是我访问 Web api 服务的方式:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://webapiservice.azurewebsites.net/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = client.GetAsync("api/values").Result;
以下是 web api 服务验证访问的方式:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = "https://tenant.onmicrosoft.com/webapiservice",
}
});
这里有什么问题吗?
谢谢
【问题讨论】:
-
不完全确定这是否会有所帮助,所以我将添加以下内容作为评论。我没有看到您在 web api 中设置受众或权限,请在此处查看示例github.com/Azure-Samples/…。此外,jwt.io 不会验证令牌签名,除非您按照以下步骤操作nzpcmad.blogspot.com.ar/2016/08/…
标签: oauth-2.0 jwt azure-active-directory bearer-token