繁体   English   中英

如何在 Blazor/ASP 应用程序中唯一标识 Active Directory 来宾用户

[英]How can I uniquely identify Active Directory guest users in a Blazor/ASP application

我将 Blazor 应用程序与 Azure Active Directory 身份验证和来宾用户一起使用。 当用户登录时,我想使用他们经过身份验证的身份作为在我自己的数据库中查找权限和其他属性的密钥。 最初我使用此代码...

@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
@code {
   var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
   var user = authState.User;

   if (user.Identity.IsAuthenticated)
   {
        //Don't do this: Name != email and may not even be constant 
        var email = user.Identity.Name; 
        var attributes = await myDatabase.FetchAttributesForUser(email);
        //use attributes.... 
   }
}

但是, user.Identity.Name不是用户的电子邮件地址。 正如这篇旧文章中所述,它只是身份验证提供程序提供的标识符。 例如,first.last@outlook.com的前景地址的用户可能会live#first.last@outlook.com名称进行身份验证,因此不能保证该名称可以是跨供应商甚至可以跨越时间,独特的同一个提供者。

这个 stackoverflow 问题确定了相同的问题,接受的答案是使用SID,但缺少的是如何从注入到我的应用程序的AuthenticationStateProvider中检索 SID 的解释。 (没有明显的字段或路径指向 SID。)我应该注入一些其他身份验证对象吗?

此外,如果建议使用 SID,我如何获取它以进行预配置? 即,我邀请“jo.blogs@contoso.com”并希望在她首次登录之前在我的数据库中设置属性。门户中显示的“对象 ID”实际上是 SID 吗?

在 Azure AD 中,您可以使用oidsub声明。 oid 声明包含用户在他们登录的 Azure AD 租户中的对象 ID。 因此,它在应用程序中是独一无二的。 sub 声明对于该用户在一个应用程序中是唯一的。

它们都是不可变的。 .NET ClaimsPrincipal可能包含具有不同名称的 oid 声明: http://schemas.microsoft.com/identity/claims/objectidentifier : http://schemas.microsoft.com/identity/claims/objectidentifier

这些备用名称的来源之一是System.Security.Claims.ClaimTypes 类

id 令牌中的声明: https : //docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims

用于从AuthenticationStateProvider获取 SID 的通用解决方案,涉及原始问题中提出的以下问题。

这个 stackoverflow 问题确定了相同的问题,接受的答案是使用 SID,但缺少的是如何从注入到我的应用程序的 AuthenticationStateProvider 中检索 SID 的说明。 (没有明显的字段或路径指向 SID。)我应该注入一些其他身份验证对象吗?

如果您注意到,AuthenticationState 的User属性会返回IIDentity类型的Identity属性,该属性还具有 WindowsIdentity 和其他身份的基本功能。 因此,将Identity转换为WindowsIdentity在这里变得有效。 转换后,您可以获取用户的 SID 信息,如下所示。 用户的其他信息可以直接从身份对象中获取。

var authenticationState = await authenticationStateTask;
WindowsIdentity identity = authenticationState.User.Identity as WindowsIdentity;

var SId = identity.User.Value;

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM