Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ASP.NET Identity

It seems this has been asked many times, in many ways, none of which seem to fit my exact situation.

Here's a line from my _LoginPartial.cshtml file:

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

See the part that says User.Identity.GetUserName()?

I want to change it to User.Identity.FirstName or User.Identity.GetFirstName().

I don't want it to say "Hello email address", but rather "Hello Bob"

My thought is that I'm simply wanting to show a new property (or method) on the Identity class. Obviously it must be more than that.

I've added the FirstName property and it is available in the AccountController.

public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

It does not get exposed in the _LoginPartial file. I want it exposed there!

Thank you for your help

like image 478
user1241166 Avatar asked Aug 19 '14 23:08

user1241166


1 Answers

Even though your answer wasn't "exactly" what I wanted, your comments led me to this solution:

@using Microsoft.AspNet.Identity
@using YourModelnameHere.Models
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @{
                var manager = new UserManager<ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(User.Identity.GetUserId()); 
            }
            @Html.ActionLink("Hello " + currentUser.FirstName + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })

            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" }) // I no longer need this ActionLink!!!
        </li>
    </ul>
    }
}

And in my IdentityModel.cs file, I added the two properties FirstName and LastName

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
like image 75
user1241166 Avatar answered Sep 23 '22 00:09

user1241166