How do I get the full name of a user in .net MVC 3

2019-01-31 03:17发布

问题:

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.

in the view,

@User.Identity.Name 

is set to DOMAIN\Username, what I want is their full Firstname Lastname

回答1:

You can do something like this:

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

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
    }
}

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" />

Add that under configuration/system.web/assemblies.



回答2:

Another option, without requiring a helper... You could just declare context and principal before you need to utilize these values, and then utilize it like a standard output...

@{ // anywhere before needed in cshtml file or view
    var context = new PrincipalContext(ContextType.Domain);
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
}

Then anywhere within the document, just call each variable as needed:

@principal.GivenName // first name
@principal.Surname // last name


回答3:

If you have many controllers then using @vcsjones approach might be painfull. Therefore I'd suggest creating extension method for TIdentity.

public static string GetFullName(this IIdentity id)
    {
        if (id == null) return null;

        using (var context = new PrincipalContext(ContextType.Domain))
        {
            var userPrincipal = UserPrincipal.FindByIdentity(context, id.Name);
            return userPrincipal != null ? $"{userPrincipal.GivenName} {userPrincipal.Surname}" : null;
        }
    }

And then you can use it in your view:

<p>Hello, @User.Identity.GetFullName()!</p>


回答4:

If you've upgraded to Identity 2 and are using claims, then this kind of info would be a claim. Try creating an extension method:

public static string GetFullName(this IIdentity id)
{
    var claimsIdentity = id as ClaimsIdentity;

    return claimsIdentity == null 
        ? id.Name 
        : string.Format("{0} {1}", 
            claimsIdentity.FindFirst(ClaimTypes.GivenName).Value, 
            claimsIdentity.FindFirst(ClaimTypes.Surname).Value);
}

Then you can use it in the view like this:

@Html.ActionLink("Hello " + User.Identity.GetFullName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })