コード例 #1
0
        public async Task <UserLoginViewModel> Handle(UserLoginQuery request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.FirstOrDefaultAsync(x => x.Email.Equals(request.Email, StringComparison.OrdinalIgnoreCase));

            if (user == null)
            {
                return new UserLoginViewModel
                       {
                           Success = false,
                           Message = "Invalid email or password."
                       }
            }
            ;

            var verified = _hashGenerator.CheckHash(user.PasswordHash, request.Password);

            if (!verified)
            {
                return new UserLoginViewModel
                       {
                           Success = false,
                           Message = "Invalid email or password."
                       }
            }
            ;

            var accessToken = _tokenGenerator.GenerateToken(user.LastName, user.Email, user.Id);

            return(new UserLoginViewModel
            {
                Success = true,
                AccessToken = accessToken,
                User = new UserInfo
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Email = user.Email,
                    UserId = user.Id,
                    Image = user.ProfilePicture != null ? user.ProfilePicture : _placeHolderImageProvider.GetProfileImagePlaceHolder()
                }
            });
        }
    }
}
コード例 #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _context.Users.SingleOrDefaultAsync(u => u.Email == Email);

            if (user == null)
            {
                ModelState.AddModelError("", "Invalid email or password.");
                return(Page());
            }

            var isPasswordValid = _hashGenerator.CheckHash(user.PasswordHash, Password);

            if (!isPasswordValid)
            {
                ModelState.AddModelError("", "Invalid email or password.");
                return(Page());
            }

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, Email),
                new Claim(ClaimTypes.NameIdentifier, Email),
                new Claim("FullName", $"{user.FirstName} {user.LastName}")
            };

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                          new ClaimsPrincipal(claimsIdentity),
                                          new AuthenticationProperties()
                                          );

            var clientApp = await _context.DeveloperApps.SingleOrDefaultAsync(x => x.AppId == Guid.Parse(AuthorizationRequest.ClientId));

            if (clientApp.RequiresConsent)
            {
                HttpContext.Session.SetString("client_id", AuthorizationRequest.ClientId);
                HttpContext.Session.SetString("response_type", AuthorizationRequest.ResponseType);
                HttpContext.Session.SetString("redirect_uri", AuthorizationRequest.RedirectUri);
                HttpContext.Session.SetString("state", AuthorizationRequest.State);
                HttpContext.Session.SetString("scope", AuthorizationRequest.Scope);
                HttpContext.Session.SetString("app_name", clientApp.AppName);

                return(RedirectToPage("/Consent/Index"));
            }


            var token = await _tokenGenerator.GenerateToken(user.Id.ToString(), user.LastName, user.Email, new List <Claim>
            {
                new Claim("client_id", clientApp.AppId.ToString()),
                new Claim("scopes", AuthorizationRequest.Scope)
            });

            var hashFragment = $"#access_token={token.AccessToken}&&expires_in={token.Expiry}&&type={token.Type}&&state={AuthorizationRequest.State}";

            return(Redirect($"{AuthorizationRequest.RedirectUri}/{hashFragment}"));
        }