我正在后端使用Angular应用程序和.net。而不是为用户创建表,我使用Microsoft Azure在Azure中重新注册应用程序后登录到应用程序。在Angular方面,我正在使用“ microsoft-adal-angular6”库连接到azure并返回令牌,并且一切正常,为了提高安全性,我将此令牌发送到了后端,该令牌也连接到Azure并验证了此令牌在显示数据之后,到这里一切都还好,我得到了令牌。
但是当用户有6个以上的组时,此令牌不具有带有ID的'groups'属性,而令牌具有的属性hasgroups:true。我在后端有例如添加新客户的策略,并且当用户有6个以上的组时,他不能添加客户,并且错误是403禁止。我尝试了很多以寻找解决方法。但是我发现并没有找到合适的例子,我需要调用图谱API来带所有组,但是我没有找到任何真实的例子,只是爆炸。因此,在这种情况下,请您告诉我如何解决这个问题。这里是我从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>', }),
角度Iterceptor看起来像:
导出类拦截器实现HttpInterceptor {
initialToken;标头=新的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):可观察> {
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
声明。您可以定义一些应用程序角色,并将角色分配给组。然后,该组中的用户将具有以下声明:
{
"roles": ["admin"]
}
然后,您可以根据用户的角色实施授权逻辑。参见参考文献here。
当然,您也可以调用Microsoft Graph API来获取组信息。
代码应类似于:
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