【发布时间】:2018-03-10 16:41:42
【问题描述】:
我有一个用 Ionic Framework 3 编写的本机客户端应用程序。我有一个用 ASP.NET Core 1.1 编写的 Web API。我想使用 Azure Active Directory 来管理对 Web API 的访问。
我在 Azure Active Directory 中注册了两个应用程序:移动应用程序和 Web API。移动应用程序具有授予访问 Web API 所需的权限。下面是来自我们的 Azure 管理员门户的权限的屏幕截图:
这在 AAD 中配置为本机应用程序。我有 AAD 提供的应用程序 ID 和对象 ID。另外,我添加了一个任意的重定向URI,我认为基于几个教程不需要解析,该URI是http://mobileCRMApp。查看 AAD 中的属性,主页 URL 为空白,注销 URL 为空白。
2017 年 10 月 3 日粗体更新:
这在 AAD 中配置为 Web 应用程序/API。我有 AAD 提供的应用程序 ID 和对象 ID。此外,我将主页 URL 和 App ID URI 都设置为匹配我的 Web API 的根 (https://crm.mycompany.com)。
我的 Ionic 客户端应用程序大致通过以下方式成功通过 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));
}
登录过程在应用程序中运行良好,回调接收到一个令牌,我可以使用 jwt.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": "pwalter@advtis.com",
"upn": "pwalter@advtis.com",
"uti": "RLvLlibQHESwmujVBBdlAA",
"ver": "1.0"
}
然后我获取令牌并将其与来自 Ionic 客户端应用程序的 API 的 http 请求一起发送,大致如下:
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 登录,我收到一个令牌,但是当我向上面带有 [Authorize] 标记的路由发送请求时,我仍然从 Web api 收到 401 未经授权的响应.
我显然在配置 API 或权限时做错了。我使用几个不同的教程把它放在一起,因为我找不到任何专门针对我的用例的东西。关于我做错了什么或如何解决问题的任何想法?
【问题讨论】:
标签: asp.net-web-api ionic-framework azure-active-directory json-web-token