我已经在Angular 2中创建了一个单页面应用程序和一个Azure MVC WebAPI,它们都需要Azure Active Directory身份验证。这两个应用程序都在Azure门户中注册,并启用了OAuth2。SPA还具有访问WebAPI的权限
在SPA中,我使用了adaljs,它的工作方式与预期一致。ASP.NET Web API被配置为使用Windows Azure Active Directory持有者身份验证,据我所知,它也是有效的。
当SPA从WebAPI请求数据时,它在报头中发送承载授权令牌,但请求被拒绝(状态401 UNAUTHORIZED)。
我已经在github:https://github.com/ranthonissen/angular2-adaljs-webapi中创建了一个示例项目,并在这里更详细地描述了我遵循的步骤:https://dotronald.be/creating-an-angular-single-page-application-with-azure-active-directory-and-adal-js-that-uses-an-asp-net-webapi/
要使此设置正常工作,我还缺少什么?
发布于 2017-04-20 13:21:26
根据代码,您正在使用clientId而不是第二个应用程序的应用程序id URI获取访问令牌。
要解决此问题,我们可以在SPA中添加资源id参数,如下所示:
secret.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class SecretService {
public get adalConfig(): any {
return {
tenant: 'adnewfei.onmicrosoft.com',
clientId: 'aac92cf9-32ab-4004-aeab-1046389dff79',
redirectUri: window.location.origin + '/',
postLogoutRedirectUri: window.location.origin + '/',
resourceId:"https://ADNewFei.onmicrosoft.com/webAPIFei"
};
}
}
resourceId API的值是您为web 注册的应用程序的应用程序ID URI。它应该与您在web API项目的web.config
中配置的值匹配,如下所示:
<appSettings>
<add key="ida:ClientId" value="29fc9b4c-4d77-4ffa-b4af-674d6b0584f7" />
<add key="ida:Tenant" value="adnewfei.onmicrosoft.com" />
<add key="ida:Audience" value="https://ADNewFei.onmicrosoft.com/webAPIFei" />
<add key="ida:Password" value="xxxxxx" />
</appSettings>
https://stackoverflow.com/questions/43495264
复制相似问题