How to add new properties to a user using Identity?

Asked

Viewed 275 times

4

How can I create new properties using the Useridentity that comes by default when creating an MVC 5 application?

Also, it is interesting to put information that does not belong directly to authentication and authorization in the Useridentity class?

In my system, in addition to user profile information, it will have a shopping cart, a list of address and movements. This information must be in Useridentity itself?

I am using Codefirst Entityframework with Dataannotations.

1 answer

2


How can I create new properties using the Useridentity that comes by default when creating an MVC 5 application?

Extending the class IdentityUser. For example:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public string Email { get; set; }
}

Also, it is interesting to put information that does not belong directly to authentication and authorization in the Useridentity class?

Yes. It all depends on the needs of your system. There is no problem in adding any properties you deem necessary for the proper functioning of your system.

In my system, in addition to user profile information, it will have a shopping cart, a list of address and movements. This information must be in Useridentity itself?

The best way to do this is to create another Model referencing the IdentityUser and in it stand the business characteristics. This is due to the fact that the IdentityUser not necessarily work synchronized with the main context of your Entity Framework application, which would require more custom code than you actually need.

There is an article in Codeproject in which this is widely discussed, including with some code on Github.

  • Gypsy as well as Identityuser does not necessarily work synchronized with Entityframework?

  • I think I expressed myself badly. Deep down he uses, but in a context separate from the general context of its application. To make both contexts work synchronized, this requires some extra code, but can be done.

  • Got it... when I use the code you gave me, it raises the Exception "Mapping and Metadata information could not be found for Entitytype[...]", you can tell if this is common?

  • 2

    This error usually refers to typos in variable names, and has nothing to do with the code I reproduced for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.