我有一个web api服务和一个web客户端应用程序来访问web api。两者都在azure活动目录中注册。 然而,当web客户端应用程序试图访问网络的API,我得到:UseJwtBearerAuthentication失败:未经授权的令牌和签名无效
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;
这里是网页API服务如何验证访问:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudience = "https://tenant.onmicrosoft.com/webapiservice",
}
});
这里有什么问题吗?
感谢
不完全确定这是否会有所帮助,因此我将添加以下内容作为注释。我没有看到你在设置观众,也没有在网络api中的权威,在这里看到一个示例https://github.com/Azure-Samples/active-directory-dotnet-webapi-getting-started/blob/master /README.md。此外,jwt.io不会验证令牌签名,除非您按照此步骤操作http://nzpcmad.blogspot.com.ar/2016/08/oauth2-verifying-azure-ad-jwt-signature.html – andresm53