我正在使用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",
}
});
这里有什么不对吗?
由于
答案 0 :(得分:0)
根据您的配置和代码段,您似乎正在尝试使用Azure AD v1端点为.Net Core设置Web API。
对于使用Azure AD v1端点的.Net Core,您应该使用UseJwtBearerAuthentication
,如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// ...
// Other stuff
// ...
app.UseJwtBearerAuthentication(
new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/{0}/",
Tenant),
Audience = ClientId
});
}
作为参考,以下是一些可以使用的其他设置:
对于使用Azure AD v1端点的.Net,您应该使用UseWindowsAzureActiveDirectoryBearerAuthentication
以下是来自官方.NET Web API sample的摘录,该示例展示了如何进行设置:
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Audience = ClientId,
Tenant = Tenant
});
}
对于使用Azure AD v2端点的.Net,您应该使用UseOAuthBearerAuthentication
,如下所示:
public void ConfigureAuth(IAppBuilder app)
{
TokenValidationParameters tvps = new TokenValidationParameters
{
// Accept only those tokens where the audience of the token is equal to the client ID of this app
ValidAudience = ClientId
};
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
// This SecurityTokenProvider fetches the Azure AD B2C metadata & signing keys from the OpenIDConnect metadata endpoint
AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(String.Format("https://login.microsoftonline.com/{0}", Tenant)))
});
}
对于使用Azure AD v2端点的.Net Core,您应该使用UseJwtBearerAuthentication
,如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// ...
// Other stuff
// ...
app.UseJwtBearerAuthentication(
new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/{0}/v2.0/",
Tenant),
Audience = ClientId
});
}