2017-05-26 153 views
0

我有一个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", 
     } 
    }); 

这里有什么问题吗?

感谢

+0

不完全确定这是否会有所帮助,因此我将添加以下内容作为注释。我没有看到你在设置观众,也没有在网络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

回答

0

根据您的配置和代码片段,它看起来像你想安装使用Azure的AD V1端点对.NET核心一个Web API。

对于.NET的核心使用Azure的AD V1终点,你应该使用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片段,样品,展示如何此设置:使用Azure的广告V2终点,你应该使用UseOAuthBearerAuthentication如下

public void ConfigureAuth(IAppBuilder app) 
{ 
    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
     new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
     { 
      Audience = ClientId, 
      Tenant = Tenant 
     }); 
} 

对于.NET:

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))) 
    }); 
} 

对于.NET的核心使用Azure的广告V2终点,你应该使用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 
     }); 
} 
+0

.net core does不直接支持UseWindowsAzureActiveDirectoryBearerAuthentication。 – derek

+0

更新了.net核心和Azure AD v1端点的答案 – Saca

+0

感谢Saca花时间回答我的问题。但是。网核没有“ConfigureAuth”功能。相反,.net核心只有“配置”,其参数是“IApplicationBuilder”而不是“IAppBuilder”。所以你的更新的答案仍然不能在.core网中工作。 – derek

相关问题