Azure AD安全API-GetAccessTokenForUserAsync不包括声明角色 [英] Azure AD Securing API - GetAccessTokenForUserAsync not include claims roles

查看:102
本文介绍了Azure AD安全API-GetAccessTokenForUserAsync不包括声明角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用API的网站.要从网站上调用API,我首先要使用GetAccessTokenForUserAsync(scope)获取一个jwt令牌.

I have a web site that calls an API. To call the API from the web site I'm getting first a jwt token using GetAccessTokenForUserAsync(scope).

我的问题是此方法无法在jwt令牌内检索用户的角色. 我的user.identity.claims有几个角色,但它们不包含在jwt令牌中.

My problem is that this method doesn't retrieve user's roles inside the jwt token. My user.identity.claims has a few roles but they are not include in the jwt token.

我想念的是什么?

我要尝试将角色包含在jwt令牌中,因为我需要保护自己的api方法.例如,一个方法只能由AdminMember调用,其他方法只能由SpecialMember调用.

I'm to trying to including the roles in the jwt token because I need to secure my api methods. For example, a method can be called only by AdminMember, other method only can be called by SpecialMember.

还有另一种方法可以在不发送用户角色的情况下保护我的api吗?

Is there another way to secure my api without sending my user's roles?

谢谢

推荐答案

由于角色是在客户端应用的注册中定义的,因此只能在其令牌中使用.

Since the roles are defined in your client app's registration, they will only be available in its tokens.

因此,基本上有两种选择:

So, there are fundamentally two options:

  1. 还要在API应用注册中定义相同的角色.您还需要在该应用上将角色分配给用户.通过使用组进行角色分配,可以使此过程变得更容易.
  2. 更改API以使用相同的应用程序注册.现在,您将在访问令牌中获得角色.使用一个应用程序注册可能意味着侵犯了最小特权,因为您想要授予该API或客户端应用程序的任何权限也将被授予另一个应用程序.

根据您的情况,您可以选择一个选项:)

Depending on your case, you can choose either option :)

这篇关于Azure AD安全API-GetAccessTokenForUserAsync不包括声明角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆