简体   繁体   中英

Get first name and last name of User.identity

I have an Intranet application set up with Windows Authentication. I need to display in the header the username and the user's initials, for example:

Welcome jSmith JS

What I did so far:

<div class="header__profile-name">Welcome <b>@User.Identity.Name.Split('\\')[1]</b></div>
<div class="header__profile-img">@User.Identity.Name.Split('\\')[1].Substring(0, 2)</div>

The issue is that the username is not always the first letter of the first name + the last name, sometimes the username can be the first name + the first letter of the last name for ex:

John Smith - the username can be jsmith but sometimes it can also be: johns

In that case my code is wrong because it will result:

jo instead of js

How can I get the full user name: first name and last name with User.identity ?

Then I'll base my code on the full user name (first and last name) in order to set up the initials and not on the username that is not always consistent.

In the ApplicationUser class, you'll notice a comment (if you use the standard MVC5 template) that says "Add custom user claims here".

Given that, here's what adding FullName would look like:

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FullName", this.FullName));
        return userIdentity;
    }
}

Using this, when someone logs in, the FullName claim will be put in the cookie. You could make a helper to access it like this:

public static string GetFullName(this System.Security.Principal.IPrincipal usr)
{
    var fullNameClaim = ((ClaimsIdentity)usr.Identity).FindFirst("FullName");
    if (fullNameClaim != null)
        return fullNameClaim.Value;

    return "";
}

Update

Or you could add it to the User's claims when you create the user and then retrieve it as a claim from the User.Identity:

await userManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));

Retreive it:

((ClaimsIdentity)User.Identity).FindFirst("FullName")

Or you could just fetch the user and access it off of the user.FullName directly:

var user = await userManager.FindById(User.Identity.GetUserId())
return user.FullName

Update

for intranet 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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