public virtual async Task RegisterAsync(NewUserArgs model) { var user = new User { UserName = model.UserName, Email = model.Email, Avatar = User.DefaultAvatar, Photo = User.DefaultAvatar, RegisteredDate = DateTime.UtcNow }; using (db.BeginTransaction()) { IdentityResult result = await userManager.CreateAsync(user, model.Password); if (!result.Succeeded) { // If user already try to register but do not confirmed, try to update data if (!result.Errors.Any(x => x.Code == "DuplicateEmail")) { throw new SunViewException(new ErrorView(result.Errors)); } user = await userManager.FindByEmailAsync(model.Email); if (user.EmailConfirmed) { throw new SunViewException(new ErrorView(result.Errors)); } user.UserName = model.UserName; user.PasswordHash = userManager.PasswordHasher.HashPassword(user, model.Password); result = await userManager.UpdateAsync(user); if (!result.Succeeded) { throw new SunViewException(new ErrorView(result.Errors)); } } else { await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString()) .UpdateAsync(); logger.LogInformation($"New user registered (id: {user.Id})"); } // Send email confirmation email var confirmToken = await userManager.GenerateEmailConfirmationTokenAsync(user); var emailConfirmUrl = globalOptions.SiteApi .AppendPathSegments("Auth", "ConfirmRegister") .SetQueryParams(new { uid = user.Id, token = confirmToken }); try { await EmailSenderService.SendEmailByTemplateAsync( model.Email, "register.html", new Dictionary <string, string> { { "[link]", emailConfirmUrl } } ); } catch (Exception exception) { throw new SunViewException(new ErrorView("EmailSendError", "Can not send email", ErrorType.System, exception)); } logger.LogInformation($"Sent email confirmation email (id: {user.Id})"); db.CommitTransaction(); } }
public virtual async Task RegisterAsync(NewUserArgs model) { var user = new User { UserName = model.UserName?.Trim(), Email = model.Email, RegisteredDate = DateTime.UtcNow }; using (db.BeginTransaction()) { IdentityResult result = await userManager.CreateAsync(user, model.Password); if (!result.Succeeded) { var error = result.Errors.FirstOrDefault(); if (result.Errors.All(x => x.Code != "DuplicateEmail")) { throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System)); } user = await userManager.FindByEmailAsync(model.Email); if (user.EmailConfirmed) { throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System)); } user.UserName = model.UserName; user.PasswordHash = userManager.PasswordHasher.HashPassword(user, model.Password); result = await userManager.UpdateAsync(user); if (!result.Succeeded) { throw new SunErrorException(new Error(error.Code, error.Description, ErrorType.System)); } } else { await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString()) .UpdateAsync(); logger.LogInformation($"New user registered (id: {user.Id})"); } // Send email confirmation email var confirmToken = await userManager.GenerateEmailConfirmationTokenAsync(user); var emailConfirmUrl = urlsOptions.CurrentValue.Api .AppendPathSegments("Auth", "ConfirmRegister") .SetQueryParams(new { uid = user.Id, token = confirmToken }); try { await emailSenderService.SendEmailByTemplateAsync( model.Email, "register.html", new Dictionary <string, string> { { "[link]", emailConfirmUrl } } ); } catch (Exception exception) { throw new SunErrorException(new Error("EmailSendError", "Can not send email", ErrorType.System, exception)); } logger.LogInformation($"Sent email confirmation email (id: {user.Id})"); db.CommitTransaction(); } }