Пример #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                if (!_captcha.Validate(Input.CaptchaCode, HttpContext.Session))
                {
                    ModelState.AddModelError("Input.CaptchaCode", "Captcha code is not correct.");
                    return(Page());
                }

                if (!await _queryHelper.CheckEmail(Input.Email))
                {
                    ModelState.AddModelError("Input.Email", "Account with this Email already exists.");

                    return(Page());
                }


                var user = new ApplicationUser {
                    UserName = Input.Email, Email = Input.Email, Login = Input.Prefix.ToLower() + Input.Login.ToLower()
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, "user");

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);


                    var acc = new Data.DataModels.Account();
                    acc.userId = user.Id;
                    acc.User   = user;
                    _db.GameAccount.Add(acc);

                    user.AccountId = acc.Id;

                    await _db.SaveChangesAsync();

                    string ssn = HelperFunctions.GenerateSSN();
                    string key = _config.GetSection("md5password").GetSection("Key").Value;

                    if (string.IsNullOrEmpty(key))
                    {
                        try
                        {
                            //await _queryHelper.InsertSSN(ssn, Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Email);
                            await _queryHelper.InsertUserAccount(Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Email);

                            await _queryHelper.InsertUserInfo(Input.Prefix.ToLower() + Input.Login.ToLower(), ssn);

                            //don't pass md5password: if you are not using md5 password in l2auth
                            await _queryHelper.InsertUserAuth(Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Password);

                            await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                              $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
                        }
                        catch (SqlException e)
                        {
                            Console.WriteLine("Errooooor: " + e.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            //await _queryHelper.InsertSSN(ssn, Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Email);
                            await _queryHelper.InsertUserAccount(Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Email);

                            await _queryHelper.InsertUserInfo(Input.Prefix.ToLower() + Input.Login.ToLower(), ssn);

                            //don't pass md5password: if you are not using md5 password in l2auth
                            await _queryHelper.InsertUserAuth(Input.Prefix.ToLower() + Input.Login.ToLower(), Input.Password, md5password : HelperFunctions.hCrypt(Input.Password, key));

                            await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                              $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
                        }
                        catch (SqlException e)
                        {
                            Console.WriteLine("Errooooor: " + e.Message);
                        }
                    }


                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl, login = Input.Prefix.ToLower() + Input.Login.ToLower(), password = Input.Password }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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