Пример #1
0
        public async Task <SignInResult> SignIn(string email, string password)
        {
            if (_accountAccess.UserExists(email))
            {
                int id = _accountAccess.GetUserId(email);

                EncryptedPassword pass = _accountAccess.GetEncryptedPassword(id);
                if (PasswordEncryptor.PasswordCorrect(password, pass))
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, email),
                        new Claim(ClaimTypes.NameIdentifier, id.ToString()),
                        new Claim("newDay", true.ToString())
                    };

                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    var authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(10),
                        IsPersistent = true,
                        IssuedUtc    = DateTime.UtcNow,
                        RedirectUri  = "./Home/Index"
                    };

                    await _httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

                    return(SignInResult.Succeeded);
                }
                else
                {
                    return(SignInResult.PasswordIncorrect);
                }
            }
            else
            {
                return(SignInResult.EmailIncorrect);
            }
        }
Пример #2
0
        public RegisterResult RegisterUser(RegisterModel user)
        {
            if (_accountAccess.UserExists(user.Email))
            {
                return(RegisterResult.EmailExists);
            }
            if (_accountAccess.UserExists("", user.Username))
            {
                return(RegisterResult.UsernameExists);
            }
            if (user.Password.Length < 4)
            {
                return(RegisterResult.PasswordShort);
            }
            if (user.Username.Length < 4)
            {
                return(RegisterResult.UsernameShort);
            }
            EncryptedPassword encryptPass = PasswordEncryptor.EncryptPassword(user.Password);

            _accountAccess.CreateUserAccount(user.Username, user.Email, encryptPass.PasswordKey, encryptPass.PasswordSalt);
            return(RegisterResult.Succeeded);
        }