【问题标题】:Detecting external Azure AD users with ASP.Net Core使用 ASP.Net Core 检测外部 Azure AD 用户
【发布时间】:2020-01-29 09:46:41
【问题描述】:

针对我的 AzureAD 租户进行身份验证的用户可以访问我的 ASP.Net Core 2.2 Web 应用程序。我可以像这样在控制器中获取他们的 UPN:this.User.FindFirst(ClaimTypes.Upn).Value

问题是来宾(外部)用户登录时。以上将失败,因为他们没有 UPN 声明。我在 User 对象上找不到任何属性或声明来表明用户是否是我的租户的成员。

检测用户是否为来宾用户的正确方法是什么?

【问题讨论】:

  • 我记不清了,但他们的家庭租户 ID 在其中一项声明中(idp、发行人等)

标签: authentication asp.net-core azure-active-directory


【解决方案1】:

正如@juunas 所说,您可以查看idp 声明:

参考:https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims

您可以获取索赔值并检查不在同一租户中的用户帐户:

var idp = User.Claims.FirstOrDefault(c => c.Type == "http://schemas.microsoft.com/identity/claims/identityprovider")?.Value;

if (idp!=null)
{
   //user is not in same tenant
}

此外,如果您想获得iss 声明,该声明标识构造并返回令牌的安全令牌服务(STS),您可以设置中间件以通过以下方式映射声明:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
    options.ClaimActions.Remove("iss");
});

并通过以下方式获取发行人:

var issuer = User.Claims.FirstOrDefault(c => c.Type == "iss")?.Value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2022-12-06
    相关资源
    最近更新 更多