0

My current code in as following.

public class ApplicationUser : IdentityUser
{
    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
        return userIdentity;
    }

}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I Want to add sum more properties in ApplicationUser class so that these fields available on views like

@User.Identity.FullName

My login action is placed at here

Community
  • 1
  • 1
vicky
  • 1,546
  • 1
  • 18
  • 35
  • You can't extend that class but you can get an instance of `ApplicationUser` (where you can add your own properties). For simple authentication something like this: `UserManager.FindById(User.Identity.GetUserId());` – Adriano Repetti Oct 26 '16 at 12:12
  • 1
    maybe [this](http://stackoverflow.com/questions/38846816/how-to-get-custom-property-value-of-the-applicationuser-in-the-asp-net-mvc-5-vie/38847016#38847016) will help – tmg Oct 26 '16 at 12:14
  • @AdrianoRepetti please check http://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity – Bharat Oct 26 '16 at 12:16
  • I believe the answer to this question solve your problem. [http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal](http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal) – Tiago Azevedo Borges Oct 26 '16 at 12:17
  • @BharatPatidar yes, you **may** do it with claims but IMo if it's the only reason to introduce them is to add a property... – Adriano Repetti Oct 26 '16 at 12:21
  • @TiagoAzevedoBorges the link is for MembershipProvider - that is different from Identity framework here. – trailmax Oct 26 '16 at 14:06

1 Answers1

1

To be able to get it the way:

1 - You need to create a Claim for that user

HttpContext.Current.GetOwinContext().GetUserManager<UserManager>().AddClaimsAsync(UserId, new Claim("FullName", "The value for the full name"));

2 - Once you add the claim for the user you can use this. I made this extension class so I would be able to get the claim value in the view.

public static class IdentityExtensions
{
    public static string FullName(this IIdentity identity)
    {
        return ((ClaimsIdentity)identity).FindFirst("FullName")?.Value;
    }
}

With this you can call it like @User.Identity.FullName()

trailmax
  • 34,305
  • 22
  • 140
  • 234
Felipe Deguchi
  • 586
  • 1
  • 7
  • 25