I'm using asp.net Identity 2.0 for users to log into my website, where the authentication details are stored in an SQL database. Asp.net Identity has been implemented in a standard way as can be found in many online tutorials.
The ApplicationUser
class in IdentityModels
has been extended to include a custom property:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
//My extended property
public string Code { get; set; }
}
When I register a new user I pass the Code
custom property in the RegisterBindingModel
but I'm not sure how to insert this custom property to the WebUsers table.
I did as bellow but it doesn't actually inserting this property to the table together with the username and password.
var user = new ApplicationUser() { UserName = userName, Email = model.Email, Code=model.Code };
And the entire function:
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var userName = !string.IsNullOrEmpty(model.UserName) ? model.UserName : model.Email;
//I set it here but it doesn't get inserted to the table.
var user = new ApplicationUser() { UserName = userName, Email = model.Email, Code=model.Code };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
What am I missing? I was looking at similar questions but couldn't find an answer for this.
Hope this might help others, since the original post is 1+years old
If have already created the project with 'Authentication Individual User Accounts:
In Solution Explorer go to project>Models>IdentityModels.cs
under
public class ApplicationUser: IdentityUser
(should be the first class).Add your custom properties after
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
like my example below:Then using NuGet Packagemanager:
I hope this helps..
If you follow all steps of adding a custom field to user, you will finish the tasks successfully.
Here is all steps to add a custom field to user:
Go to Models folder → Open IdentityModels.cs → ApplicationUser class and add the property:
Type
Enable-Migrations
and press Enter and wait until the task get completed. You will see a response which says:Type
Add-Migration "Code"
and press Enter and wait until the task get completed. You will see a response which says:Type
Update-Database
and press Enter and wait until the task get completed. You will see a response which says:At this step if you refresh SQL Server Object Explorer and go to database and see tables, under
dbo.AspNetUsers
under columns, you will see theCode
field. If you didn't know which database or even which server you should look for, open Web.Config file and take a look at connection string which is something like this:You can see data source (which is sql server instance) and something .mdf which is database name.
Go to Models folder → Open AccountViewModels.cs file → RegisterViewModel class and add this property: (In APIv2 with EF6, you can add the below line in Models folder → AccountBindingModels file → RegisterBindingModel class)
Go to Views folder → Account folder → Open Register.cshtml file and add this code near other fields, for example below password:
Go to Controllers folder → Open AccountController.cs file → in http post Register action, change the line which creates user to this:
Run project and go to
/Account/Register
url and register a new user. After registering the user, if you go to database again and View Data of dbo.AspNetUsers table, you will see the code has been saved.Download
You can clone or download a working example here:
Further reading - How to Add a custom Property to IdentityRole?
If you are interested to know how to add a new property to
IdentityRole
, take a look at How to Add a custom Property to IdentityRole?