Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identity 2.0 _LoginPartial First Name

My application is MVC5 c#, I have extended the ApplicationUser model to include First and last name, works well. I am trying to figure out how I can change the loginPartial to display the user actual first name rather than their email address in the following code:

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
like image 882
hncl Avatar asked Nov 23 '25 05:11

hncl


1 Answers

The above was my question, unfortunately I could not log with my old account. Here how I did it:

In the Account Controller / Login I added the following:

            var user = await UserManager.FindByNameAsync(model.UserName);
            var t = await UserManager.AddClaimAsync(user.Id, new Claim("FullName", user.FirstName + " " + user.LastName));

Add this class:

public static class GenericPrincipalExtensions
    {
    public static string FullName (this IPrincipal user)
        {
        if (user.Identity.IsAuthenticated)
            {

            var claimsIdentity = user.Identity as ClaimsIdentity;
            if (claimsIdentity != null)
                {
                foreach (var claim in claimsIdentity.Claims)
                    {
                    if (claim.Type == "FullName")
                        return claim.Value;
                    }
                }
            return "";
            }
        else
            return "";
        }
    }

Please see the comments above by Brendan Green, thank you Brendan for the lead. Changed the _LoginPartial to:

 @Html.ActionLink("Hello " + User.FullName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new {title = "Manage" })
like image 70
hncl Avatar answered Nov 25 '25 19:11

hncl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!