Introduction
In order to create a new user account in ASP.NET Identity, we use the Create method.
Create
The Create method creates the user passed as a parameter of the method with the given password and returns
the result of the operation as an IdentityResult
object.
IdentityResult result = UserManager.Create(user, model.Password);
The Create method is called by the UserManager which is responsible for performing the user-related operations.
Note : you can also create a user account with no password.
IdentityResult result = UserManager.Create(user);
UserManager
We use the GetUserManager method to get the ApplicationUserManager from the OWIN context.
ApplicationUserManager UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
We use the ApplicationUserManager instead of the UserManager in order to use
the default validation logic for usernames and passwords configured in the IdentityConfig.cs
file.
Namespaces
In order to use the GetUserManager method, the Create method and the IdentityResult object, you have to include the following namespaces :
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
Example
[HttpPost]
public ActionResult AddUser(RegisterUserViewModel model)
{
if (ModelState.IsValid) {
ApplicationUser user = new ApplicationUser
{
FirstName = model.FirstName,
LastName = model.LastName,
PhoneNumber = model.PhoneNumber,
Email = model.Email,
UserName = model.UserName
};
ApplicationUserManager UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
IdentityResult result = UserManager.Create(user, model.Password);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
foreach (string error in result.Errors)
ModelState.AddModelError("", error);
}
return View(model);
}
In the above example, we get the information of the user to create via an HTTP Post request. We get
the user manager from the OWIN context by using the GetUserManager method and then we use
the Create method in order to create this new user. We access the Succeeded
property of
the IdentityResult object to check if the opration was successful. If it is not the case, we loop
through the list of Errors
and we add them to the ModelState by using the AddModelError
method.
The RegisterUserViewModel
object is the ViewModel that we are using in order to render and validate the
AddUser view.
public class RegisterUserViewModel
{
[Display(Name = "Username")]
[Required]
public string UserName { get; set; }
[Display(Name = "First name")]
public string FirstName { get; set; }
[Display(Name = "Last name")]
public string LastName { get; set; }
[Display(Name = "Email")]
[Required]
[EmailAddress]
public string Email { get; set; }
[Display(Name = "Phone number")]
[Phone]
public string PhoneNumber { get; set; }
[Display(Name = "Password")]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
Get the ID of the newly created user
By instantiating a new ApplicationUser
object, the base constructor generates and populates the Id
field.
So after checking that the user was successfully created, we can safely access its Id
.
if (identityResult.Succeeded)
{
string UserId = user.Id;
return RedirectToAction("Index", "Home", new { Id = UserId});
}