在ASP.Net中使用Azure Active Directory令牌使用UseJwtBearerAuthenticiation的核心Web API
我有一个用Ionic Framework 3编写的本地客户端应用程序。我有一个用ASP.NET Core 1.1编写的Web API。我想使用Azure Active Directory来管理对Web API的访问。在ASP.Net中使用Azure Active Directory令牌使用UseJwtBearerAuthenticiation的核心Web API
我已经使用Azure Active Directory注册了两个应用程序:移动应用程序和Web API。移动应用程序具有授予访问Web API所需的权限。下面是我们的Azure的管理门户网站的权限的屏幕截图:
此配置为原生应用AAD。我有一个由AAD提供的应用程序ID和对象ID。此外,我添加了一个任意的重定向URI,我认为基于几个教程不需要解决,该URI是http://mobileCRMApp
。查看AAD中的属性,主页URL为空白,注销URL为空。
BOLD修订版2017年10月3日:
这被配置为在一个AAD Web应用程序/ API。我有一个由AAD提供的应用程序ID和对象ID。此外,我设置了主页URL和应用程序ID URI以匹配我的Web API(https://crm.mycompany.com
)的根目录。
我离子客户端应用程序成功认证对AAD大致按以下方式:
authenticate(userID, authCompletedCallback) : any {
let parent = this;
//this.context = new AuthenticationContext(this.config.authority);
let context = this.msAdal.createAuthenticationContext("https://login.microsoftonline.com/myTenantId");
console.log(context);
context.acquireTokenAsync(parent.config.resourceUri, parent.config.clientId, parent.config.redirectUri, userID, "")
.then(authCompletedCallback)
.catch((e: any) => console.log('Authentication failed', e));
}
登录过程中的应用程序去罚款,回调接收的令牌,我可以用智威汤逊翻译它的有效载荷。 IO到大致有以下几种:
{
"aud": "https://crm.mycompany.com/",
"iss": "https://sts.windows.net/someID/",
"iat": 1506539211,
"nbf": 1506539211,
"exp": 1506543111,
"acr": "1",
"aio": "someOtherID",
"amr": [
"pwd"
],
"appid": "appID",
"appidacr": "0",
"e_exp": 262800,
"family_name": "Walter",
"given_name": "Philip",
"ipaddr": "someAddress",
"name": "Philip Walter",
"oid": "someOtherID",
"onprem_sid": "someOtherID",
"puid": "stuff",
"scp": "user_impersonation",
"sub": "e_X7WlAoVS2vzXm1pr3kcDOrET7czcC0f8-YRU_2DJ8",
"tenant_region_scope": "NA",
"tid": "ourTenantID",
"unique_name": "[email protected]",
"upn": "[email protected]",
"uti": "RLvLlibQHESwmujVBBdlAA",
"ver": "1.0"
}
于是我把令牌和一个HTTP请求从离子客户端应用程序的API一起发送,大致是这样:
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + this.authToken.accessToken);
let options = new RequestOptions({ headers : headers });
this.data.http.get(url, options).map(res => res.json()).subscribe((data) => {
console.log(data);
});
的API,然后在Startup.cs以下
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Authority = Configuration["Authentication: AzureAd:AADInstance"] + Configuration["Authentication: AzureAd:TenantId"],
Audience = Configuration["Authentication: AzureAd:Audience"]
});
所以,我可以通过在客户端应用程序AAD登录,我收到一个道理,但我仍然得到一个401未经授权响应当我发送一个请求到一个带有[Authorize]标签的路由上的web api时。
我显然在配置API或权限时出错。我使用几个不同的教程将它们放在一起,因为我找不到具体解决我的用例的任何事情。任何想法,我做错了什么,或者我可能会排除故障?
为什么你的听众说"aud": "https://graph.windows.net"
?
它应该说是"aud": "https://yourWebApi.azurewebsites.net"
或类似的东西。
上面包含的令牌只适用于Graph API,任何其他Azure AD受保护资源都会拒绝它,包括您的Web API,因为它预计受众与自己匹配。
parent.config.resourceUri
可能是在您源为目标受众群体:
context.acquireTokenAsync(
parent.config.resourceUri,
...
根据RFC 7519:
The
"aud"
(audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the "aud" claim when this claim is present, then the JWT MUST be rejected. In the general case, the "aud" value is an array of case- sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the "aud" value MAY be a single case-sensitive string containing a StringOrURI value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.
有时这就是所谓的资源,有时观众,有时澳元。 ..它是什么:)
我尝试将'parent.config.resourceUri'更改为我尝试访问的Azure资源(Web api)的App ID URI。我现在得到一个带有'aud'的令牌,与我的本地应用程序的'clientId'相等。对我的Web API的请求仍然得到401拒绝。我是否以正确的方式去做这件事?我有两个注册Azure的应用程序:nativeApp和WebAPI。 nativeApp已委派访问WebAPI的权限。 nativeApp从AAD获取如上所述的令牌,然后在向WebAPI发出请求时显示该令牌。这是正确的流程吗?如果是这样,我仍然必须有这些URI的错误? –
ClienId是一个GUID。 aud应该是一个URL(https:// ..)。你在aud中有URL吗? – evilSnobu
Parent.config.ResourceUri应该是'https:// yourweb.api.com'或任何其他位置。 – evilSnobu