我如何在.NET MVC 3应用程序内网用户的全名? [英] How do I get the full name of a user in .net MVC 3 intranet app?

查看:102
本文介绍了我如何在.NET MVC 3应用程序内网用户的全名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对特定领域进行Windows身份验证的MVC 3 Intranet应用程序。我想呈现当前用户的名称。

I have an MVC 3 intranet application that performs windows authentication against a particular domain. I would like to render the current user's name.

在视图中,

@User.Identity.Name 

设置为域\\用户名,我要的是其全部名字姓氏

推荐答案

您可以做这样的事情:

using (var context = new PrincipalContext(ContextType.Domain))
{
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
    var firstName = principal.GivenName;
    var lastName = principal.Surname;
}

您将需要添加到 System.DirectoryServices.AccountManagement 集的引用。

You'll need to add a reference to the System.DirectoryServices.AccountManagement assembly.

您可以添加剃刀帮手,像这样:

You can add a Razor helper like so:

@helper AccountName()
    {
        using (var context = new PrincipalContext(ContextType.Domain))
    {
        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
        @principal.GivenName @principal.Surname
    }
}

如果您indend从视图这样做,而不是控制器,您需要添加的程序集引用到你的web.config,以及:

If you indend on doing this from the view, rather than the controller, you need to add an assembly reference to your web.config as well:

<add assembly="System.DirectoryServices.AccountManagement" />

添加,根据配置/ System.Web程序/组件

这篇关于我如何在.NET MVC 3应用程序内网用户的全名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆