asp.net-core - 如何从@context.User 获取用户的名字或姓氏?

标签 asp.net-core razor active-directory blazor asp.net-authorization

我使用 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/

相关文章:

.net - Active Directory 事务感知吗?

c# - WinHttp异常 : The connection with the server was terminated abnormally

azure - ASP.NET Core token 获取异常

visual-studio - IHostingEnvironment.EnvironmentName如何工作?

c# - 使用托管标识从 Azure 应用服务调用图

c# - 在事件目录中查找计算机

templates - Knockout.js 模板未更新 dependentObservable 上的 UI 绑定(bind)

c# - 更改 DropDownList 选项时使用参数重定向到 MVC ActionResult

asp.net-mvc - 使用 RenderSection 从部分 View 在 cshtml 中注入(inject) javascript 不起作用

azure - 无法通过 ARM 模板启动 AD Forest