public async Task <ActionResult <MmtUserGetDTO> > PostMmtUser([FromForm] MmtUserPostDTO model) { var mmtUser = await _UserManager.FindByEmailAsync(model.Email); if (mmtUser != null) { ModelState.AddModelError("", "there is already a user with the same Email"); return(BadRequest(ModelState)); } if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("", "password doent match his confirmation"); return(BadRequest(ModelState)); } mmtUser = new MmtUser { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, PostalCode = model.PostalCode, City = model.City, Country = model.Country, PhoneNumber = model.Mobile, PhoneHome = model.PhoneHome, PhoneWork = model.PhoneWork, Function = model.Function, Street = model.Street, EmailConfirmed = model.ConfirmEmail, }; var result = await _UserManager.CreateAsync(mmtUser, model.Password); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(BadRequest(ModelState)); } if (model.Roles != null) { await _UserManager.AddToRolesAsync(mmtUser, model.Roles); } var mmtUserGetDTO = new MmtUserGetDTO { Id = mmtUser.Id, UserName = mmtUser.UserName, FirstName = mmtUser.FirstName, LastName = mmtUser.LastName, Email = mmtUser.Email, PostalCode = mmtUser.PostalCode, City = mmtUser.City, Country = mmtUser.Country, Mobile = mmtUser.PhoneNumber, PhoneHome = mmtUser.PhoneHome, PhoneWork = mmtUser.PhoneWork, Function = mmtUser.Function, Street = mmtUser.Street, }; var allRoles = _RoleManager.Roles; var rolsAsString = await _UserManager.GetRolesAsync(mmtUser); List <IdentityRole> roles = new List <IdentityRole>(); foreach (var item in rolsAsString) { foreach (var role in allRoles) { if (item == role.Name) { roles.Add(role); } } } //mmtUserGetDTO.Roles = await _UserManager.GetRolesAsync(mmtUser); mmtUserGetDTO.Roles = roles; return(Ok(mmtUserGetDTO)); }
//logic to register a user public async Task <UserManagerResponse> RegisterUserAsync(RegisterViewModel model) { if (model == null) { throw new NullReferenceException("model is null"); } if (model.Password != model.ConfirmPassword) { return new UserManagerResponse { Result = "Confirm password dont match password", IsSucces = false, } } ; //i should realy user automapper hier -lot of properties to manualy map var mmtUser = new MmtUser { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, City = model.City, Country = model.Country, Function = model.Function, PhoneHome = model.PhoneHome, PhoneWork = model.PhoneWork, PhoneNumber = model.Mobile, Street = model.Street, PostalCode = model.PostalCode, }; //here we create a user and add it to aspnetUser table var result = await _UserManager.CreateAsync(mmtUser, model.Password); if (!result.Succeeded) { return(new UserManagerResponse { Result = "failed to create the user", IsSucces = false, Errors = result.Errors.Select(e => e.Description), }); } await _UserManager.AddToRoleAsync(mmtUser, "user"); //here we generate an email confirmation token, identity wil take care of that var confirmEmailTokenn = await _UserManager.GenerateEmailConfirmationTokenAsync(mmtUser); //we need to convert the token to an array of bytes to we able to encode it to base64 var encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailTokenn); //we encode the token to base64 to get rid of all special charcters that the browsers doesnt accept in the url, because wil wil send the token in the url var validEmailToken = WebEncoders.Base64UrlEncode(encodedEmailToken); string url = $"{_Configuration["AppUrl:Dev"]}/api/auth/confirmEmail?userid={mmtUser.Id}&token={validEmailToken}"; //sending the email to the user await _MailService.SendEmailAsync(mmtUser.Email, "Confirm your email", $"<h1>Welcome to Mmt</h1>" + $"<p>Please confirm your email by <a href={url}>Clicking here</a></p>"); return(new UserManagerResponse { Result = $"The user : {model.UserName} has been created successfully", IsSucces = true, }); }