4

I'm building a Web API (2) project and use "individual account" authentication. I want to

  1. extend user with some details (like first name/last name, etc) (in model)
  2. obtain that info after login (on client side - my case windows phone)

I've just started learning MVC and Web API so can anybody help me on this?

Let me explain myself a little bit: I have create webapi project and selected as authentication method - "inidividual accounts" well I've added a model class called Person with 2 fields: FirstName and LastName. Well I've used fiddler to register a user called "johndoe" with a password. After this I've used fiddler to authenticate this user at "server:port/Token" and got bearer token. Well Here is a problem, I need to know how to associate this user class with my Person class in model, and second I do not know how to write a controller so when will send a get request our controller function will return associated Person.

tereško
  • 58,060
  • 25
  • 98
  • 150
Sergiu Cojocaru
  • 687
  • 6
  • 16

1 Answers1

11

First, as in : Introduction to ASP.NET Identity

By default, the ASP.NET Identity system stores all the user information in a database. ASP.NET Identity uses Entity Framework Code First to implement all of its persistence mechanism.

Now,the IdentityUser class will be mapped to AspNetUsers Table in Database,so lets extend it by create a new class that inherit from it and add our custom properties:

public class CustomUser : IdentityUser
{
    public string Email { get; set; }
}

Then you should let the UserManager class to use CustomUser instead of IdentityUser Class,so in Startup.cs ,change the generic type for UserManager and UserStore to be like:

UserManagerFactory = () => new UserManager<CustomUser>(new UserStore<CustomUser>());

also the property :

public static Func<UserManager<CustomUser>> UserManagerFactory { get; set; }

In ApplicationOAuthProvider.cs and any place in your solution do the same changes for generic type for UserManager,and start using CustomUser instead of IdentityUser . You can add the new "Email" property to RegisterBindingModel as :

public class RegisterBindingModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public string Email { get; set; }
}

Then in registration you can add value:

 CustomUser user = new CustomUser
        {
            UserName = model.UserName,
            Email=model.Email
        };

I hope it will help

UPDATE

If anyone got an ExceptionMessage "Invalid column name 'Discriminator'." that means the "AspNetUsers" is already created before following my steps ,and the table doesn't have "Discriminator" column, so all what you should do is going to database,change the design of AspNetUsers table by adding a new column with type as nvarchar(128)(Also add a new column that you want to have like Email in my example),and name it Discriminator , and don't forget to fill the fields of this column for old records with the appropriate value (in my example it will be CustomUser).

Yasser Sinjab
  • 578
  • 6
  • 19
  • 1
    I get an exception with the ExceptionMessage "Invalid column name 'Discriminator'." :( – Fred Jan 28 '14 at 12:02
  • @Fred did you solve that Discriminator error? I am getting it now :( – Laurence Mar 03 '14 at 15:53
  • @Laurence Nyein Could you please explain to me when did you get this exception ,and in which controller? – Yasser Sinjab Mar 04 '14 at 08:27
  • Thanks @YasserSinjab .. It's fine now after recreating the AspNetUsers table. – Laurence Mar 04 '14 at 09:49
  • also is it possible to remove the discriminator column? its not needed in context of acl. – Jaime Sangcap Sep 25 '14 at 08:50
  • @Daskul actually i think you can't delete it because in this version of Asp Identity it uses TPH when you inherit from IdentityUser ,but in the latest version of Microsoft.AspNet.Identity.EntityFramework ,you can add custom properties to ApplicationUser which inherit from IdentityUser and it will not generate discriminator column in AspNetUsers table – Yasser Sinjab Sep 25 '14 at 09:43