我使用 Windows 身份验证创建了一个服务器端 Blazor 应用程序。它创建了以下文件。
登录Display.razor
<AuthorizeView>
Hello, @context.User.Identity.Name!
</AuthorizeView>
但是,它显示“域\用户名”。它是一种显示用户名字的方法吗?
我试图打印所有的 claim 类型。
@foreach(var c in context.User.Claims)
{
<p>@c.Value @c.Type</p>
}
但是,只有一种类型带有名称。 (值为
DOMAIN\username
)http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
最佳答案
您还可以使用目录服务(使用 NuGet 安装),如下所示使用它。
_Imports.razor
@using System.DirectoryServices.AccountManagement
登录Display.razor
<AuthorizeView>
@{
var pcontext = new PrincipalContext(ContextType.Domain, "XYZ.net", "DC=XYZ,DC=net");
var principal = UserPrincipal.FindByIdentity(pcontext, context.User.Identity.Name);
}
Hello, @principal.GivenName @principal.Surname!
</AuthorizeView>
关于asp.net-core - 如何从@context.User 获取用户的名字或姓氏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457531/