【问题标题】:Getting User Role from Azure从 Azure 获取用户角色
【发布时间】:2018-02-22 15:01:20
【问题描述】:

我创建了一个简单的 Asp.net Web 应用程序项目并使用 Azure Web App 服务将其发布到 Azure,我还设置了身份验证,但我是从 Azure 完成的。在 Azure Web 应用中,您可以选择使用 azure Ad 设置身份验证。

在我的应用程序中,我只是提供了一个带有链接 mydomain/.auth/logout 的注销按钮,它工作正常。在我的应用程序中,没有任何身份验证代码。

有没有一种方法可以让我获得登录我的应用程序的用户角色(我在我的天蓝色应用程序注册清单中设置了 2 个角色,并将这些角色分配给了用户)。我已经搜索过,但我无法找到它。当我使用 owin 和 open-id-connect 通过代码设置身份验证过程时,我能够做到这一点,但我不想那样做。有什么方法可以在我的应用程序中不编写身份验证代码的情况下做到这一点?

【问题讨论】:

  • 如果你能得到它,那些应该在 id 令牌中。

标签: azure authentication azure-active-directory


【解决方案1】:

嗯,我认为有两种方法可以获取登录用户的角色但是我不确定它们是否可以在您的场景中使用。

  1. 正如juunas所说,您可以从id_token获取信息。我测试了一下,可以解码id_token得到角色:

  1. 如果您知道该用户的主要显示名称,您可以通过 AAD Powershell 获取角色,这是我的脚本和结果:

Get-AzureADServicePrincipal -ObjectId "<the ObjectId of that SP>"| % {    
      $appRoles = @{ "$([Guid]::Empty.ToString())" = "(default)" }
      $_.AppRoles | % { $appRoles[$_.Id] = $_.DisplayName }
      Get-AzureADServiceAppRoleAssignment -ObjectId ($_.ObjectId) | Select ResourceDisplayName, PrincipalDisplayName,  Id | Where-Object {$_.PrincipalDisplayName -eq "yangsa666@outlook.com" } | % {  $_ | Add-Member "AppRoleDisplayName" $appRoles[$_.Id] -Passthru
      }
    }

希望这会有所帮助!

【讨论】:

  • 我尝试在 powershell 中运行它说 The term 'Get-AzureADServicePrincipal' is not Recognized as the name of a cmdlet
  • 嗨,@RahulGupta。是否安装了Azure AD Powershell
  • :) 它正在工作,但是当我输入对象 id 时,它说资源不存在,在对象 id 中,我将我们在 azure ad 中注册应用程序时获得的对象 id .....我尝试使用对象 ID 和 App Id
  • 可能是错误的对象 ID。您需要找到服务主体对象 ID。那就是“本地目录中的应用程序”/企业应用程序。
  • 它不能通过代码工作,因为要使用它,您需要先连接到 AzureAd,但对我来说,身份验证不是通过代码完成的
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 2018-09-03
  • 2013-05-21
  • 2020-09-19
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多