コード例 #1
0
 public static string SHA1(string plainTextString, byte[] salt)
 {
     if (data == null)
     {
         data = System.Security.Cryptography.SHA1.Create();
     }
     return(UserDatabaseHelper.ByteArrayToString(data.ComputeHash(Combine(Encoding.UTF8.GetBytes(plainTextString), salt))));
 }
コード例 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (model.Email != "*****@*****.**")
            {
                ModelState.AddModelError(string.Empty, "Registration is closed. Try again later");

                return(View(model));
            }
            if (ModelState.IsValid)
            {
                //var verifyEmail = KickBoxAPI.CheckEmail(model.Email);
                var verifyEmail = new KickBoxEmailResult
                {
                    Result = "success"
                };
                if (verifyEmail.Result.ToLower() == "undeliverable")
                {
                    if (string.IsNullOrWhiteSpace(verifyEmail.DidYouMean))
                    {
                        ModelState.AddModelError(string.Empty, "Unable to verify your email.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty,
                                                 $"Unable to verify your email. Is your email actually: {verifyEmail.DidYouMean}");
                    }
                    return(View(model));
                }
                else if (verifyEmail.Result.ToLower() == "risky")
                {
                    ModelState.AddModelError(string.Empty,
                                             "There was an error with your email. Please try another email.");
                    return(View(model));
                }
                var user = new ApplicationUser {
                    UserName = model.Username, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var salt = UserDatabaseHelper.MakeSalt();
                    UserDatabaseHelper.CreateUser(model.Username, user.Id,
                                                  SHA1(model.Password + StaticVars.pepper, salt), model.Email, salt);

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                                                      $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }