Azure 令牌 hasgroups:true,Adal angular,Graph API,
Azure token hasgroups: true ,Adal angular, Graph API,
我正在后端开发 Angular 应用程序和 .net。
我没有为用户创建 table,而是在 Azure 中注册后使用 Microsoft Azure 登录应用程序。
在 Angular 方面,我使用 'microsoft-adal-angular6' 库连接到 azure 和 return 令牌,一切正常,为了更安全,我将此令牌发送到后端,它也连接到 Azure 并在显示数据后验证此令牌,直到这里一切正常,我得到了令牌。
但是当用户拥有超过 6 个组时,此令牌没有 属性 'groups' 和 Id,取而代之的是,令牌具有 属性 hasgroups:true。
我在后端有策略,例如添加新客户,当用户有超过 6 个组时,他不能添加客户,错误是 403 forbiden。
我努力寻找灵魂。但在我的情况下我没有找到合适的,我需要调用 graph API 来带来所有组,但我没有找到任何真实的例子,只是 explination。
那么,在这种情况下,您能否告诉我如何解决这个问题。
这是我如何从 Angular.
连接到 azure 的代码
Angular :
appModule.ts :
imports: [ MsAdalAngular6Module.forRoot({
tenant: 'tetant Id of the app in Azure',
clientId: 'client Id of the app in Azure',
redirectUri: window.location.origin,
endpoints: {
"http://localhost:4211/": "http://localhost:4211/",
},
navigateToLoginRequestUrl: false,
cacheLocation: '<localStorage / sessionStorage>', }),
Angular 拦截器看起来像:
export class 拦截器实现 HttpInterceptor {
初始令牌;
headers = new HttpHeaders({ 'Content-Type': 'application/json' });
constructor( private adalSvc: MsAdalAngular6Service,private http:HttpClient,private router:Router)
{
this.initialToken = sessionStorage.getItem('adal.idtoken') ;
console.log( "initialToken " , this.initialToken);
}
拦截(请求:HttpRequest,下一个:HttpHandler):Observable> {
if (this.initialToken) {
request = request.clone({
setHeaders: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.initialToken}`
}
});
}
return next.handle( request ).pipe( tap(() => {},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status !== 401) {
return;
}
this.router.navigate(['']);
}
}))
}
后端验证令牌:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Audience = "clientId";
options.Authority = "https://login.microsoftonline.com/tetantId/";
options.RequireHttpsMetadata = false;
options.Events = new JwtBearerEvents()
{
OnTokenValidated = context =>
{
return Task.CompletedTask;
}
};
});
感谢您的帮助。
如果您使用隐式流并且用户有 6 个或更多组,则它们不会在访问令牌中。由于令牌在 URL.
中返回,因此隐式流的限制较小
这种情况下,不建议使用groups
声明。您可以定义一些应用程序角色并将这些角色分配给组。那么群里的用户就会有如下的claim:
{
"roles": ["admin"]
}
然后您可以根据用户的角色实现您的授权逻辑。请参阅参考资料 here。
当然你也可以调用Microsoft GraphAPI获取群组信息
代码应该是这样的:
if (hasGroups)
Call the Graph to inquire:
Either about the full group membership OR
About membership to a particular group
else
Access groups directly from the token
看到一个sample for Angular apps with Microsoft Graph. For more samples, see here。
我正在后端开发 Angular 应用程序和 .net。 我没有为用户创建 table,而是在 Azure 中注册后使用 Microsoft Azure 登录应用程序。 在 Angular 方面,我使用 'microsoft-adal-angular6' 库连接到 azure 和 return 令牌,一切正常,为了更安全,我将此令牌发送到后端,它也连接到 Azure 并在显示数据后验证此令牌,直到这里一切正常,我得到了令牌。
但是当用户拥有超过 6 个组时,此令牌没有 属性 'groups' 和 Id,取而代之的是,令牌具有 属性 hasgroups:true。 我在后端有策略,例如添加新客户,当用户有超过 6 个组时,他不能添加客户,错误是 403 forbiden。 我努力寻找灵魂。但在我的情况下我没有找到合适的,我需要调用 graph API 来带来所有组,但我没有找到任何真实的例子,只是 explination。 那么,在这种情况下,您能否告诉我如何解决这个问题。 这是我如何从 Angular.
连接到 azure 的代码Angular :
appModule.ts :
imports: [ MsAdalAngular6Module.forRoot({
tenant: 'tetant Id of the app in Azure',
clientId: 'client Id of the app in Azure',
redirectUri: window.location.origin,
endpoints: {
"http://localhost:4211/": "http://localhost:4211/",
},
navigateToLoginRequestUrl: false,
cacheLocation: '<localStorage / sessionStorage>', }),
Angular 拦截器看起来像:
export class 拦截器实现 HttpInterceptor {
初始令牌; headers = new HttpHeaders({ 'Content-Type': 'application/json' });
constructor( private adalSvc: MsAdalAngular6Service,private http:HttpClient,private router:Router)
{
this.initialToken = sessionStorage.getItem('adal.idtoken') ;
console.log( "initialToken " , this.initialToken);
}
拦截(请求:HttpRequest,下一个:HttpHandler):Observable> {
if (this.initialToken) {
request = request.clone({
setHeaders: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.initialToken}`
}
});
}
return next.handle( request ).pipe( tap(() => {},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status !== 401) {
return;
}
this.router.navigate(['']);
}
}))
}
后端验证令牌:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Audience = "clientId";
options.Authority = "https://login.microsoftonline.com/tetantId/";
options.RequireHttpsMetadata = false;
options.Events = new JwtBearerEvents()
{
OnTokenValidated = context =>
{
return Task.CompletedTask;
}
};
});
感谢您的帮助。
如果您使用隐式流并且用户有 6 个或更多组,则它们不会在访问令牌中。由于令牌在 URL.
中返回,因此隐式流的限制较小这种情况下,不建议使用groups
声明。您可以定义一些应用程序角色并将这些角色分配给组。那么群里的用户就会有如下的claim:
{
"roles": ["admin"]
}
然后您可以根据用户的角色实现您的授权逻辑。请参阅参考资料 here。
当然你也可以调用Microsoft GraphAPI获取群组信息
代码应该是这样的:
if (hasGroups)
Call the Graph to inquire:
Either about the full group membership OR
About membership to a particular group
else
Access groups directly from the token
看到一个sample for Angular apps with Microsoft Graph. For more samples, see here。