Exemplo n.º 1
0
        public async Task <IActionResult> SignUp(SignUpUser user)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(user.ID) && !string.IsNullOrEmpty(user.Name) && !string.IsNullOrEmpty(user.Password) && !string.IsNullOrEmpty(user.CheckPassword))
                {
                    if (user.Password != user.CheckPassword)
                    {
                        ViewBag.ErrorInfo = "Created and confirmed passwords are not the same";
                        return(View(user));
                    }
                    else if (!await _signUpService.CheckId(user.ID))
                    {
                        if (await _signUpService.SignUpAsync(user))
                        {
                            await HttpContext.SignOutAsync();

                            var claimIdentity = new ClaimsIdentity("Cookie");
                            claimIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.ID));
                            claimIdentity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
                            claimIdentity.AddClaim(new Claim("Avatar", "Default.png"));
                            claimIdentity.AddClaim(new Claim(ClaimTypes.Role, "Commom"));

                            var claimsPrincipal = new ClaimsPrincipal(claimIdentity);
                            await HttpContext.SignInAsync(claimsPrincipal);

                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            ViewBag.ErrorInfo = "注册过程存在异常,请进行调试";
                        }
                    }
                    else
                    {
                        ViewBag.ErrorInfo = "UserId is exist";
                    }
                }
                else
                {
                    ViewBag.ErrorInfo = "Information is incomplete";
                }
            }
            return(View(user));
        }