/// <summary> /// Converts a user request model into an entity model /// </summary> /// <param name="user"></param> /// <returns></returns> public ApplicationUser Create(ApplicationUserRequestModel user) { Person p = new Person { FirstName = user.FirstName, LastName = user.LastName, JoinDate = DateTime.Now, Level = user.Level }; return(new ApplicationUser { UserName = user.Username, Email = user.Email, Person = p }); }
public async Task <IHttpActionResult> register(ApplicationUserRequestModel model) { /*---------------------------------------------------------------------------- * Validate request model * ----------------------------------------------------------------------------*/ if (!ModelState.IsValid) { return(BadRequest(ModelState)); } /*---------------------------------------------------------------------------- * Check if user already exists * ----------------------------------------------------------------------------*/ var existingUser = await this.AppManager.FindByNameAsync(model.Username); if (existingUser != null) { return(BadRequest("Username already exists")); } /*---------------------------------------------------------------------------- * Check if role already exists * ----------------------------------------------------------------------------*/ var role = await this.RoleManager.FindByNameAsync(model.RoleName); if (role == null) { return(BadRequest("Invalid role")); } /*---------------------------------------------------------------------------- * Create user * ----------------------------------------------------------------------------*/ var user = UserModelFactory.Create(model); user.EmailConfirmed = true; // For this exercise, we will auto confirm user (comment out if you want to use email confirmation) IdentityResult addUserResult = await this.AppManager.CreateAsync(user, model.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } /*---------------------------------------------------------------------------- * Add user to role * ----------------------------------------------------------------------------*/ await this.AppManager.AddToRoleAsync(user.Id, role.Name); /*---------------------------------------------------------------------------- * Send confirmation email * ----------------------------------------------------------------------------*/ /* * // UNCOMMENT THIS IF YOU WANT TO FORCE USER TO CONFIRM THEIR EMAIL * string code = await this.AppManager.GenerateEmailConfirmationTokenAsync(user.Id); * var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code })); * await this.AppManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); */ /*---------------------------------------------------------------------------- * Return 'OK' with user uri * ----------------------------------------------------------------------------*/ Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return(Created(locationHeader, UserModelFactory.Create(user))); }