Пример #1
0
        public async Task <AccessRefreshDto> GetJwtToken(CustomIdentityUserDto userAuthenticationInfo,
                                                         CancellationToken ct)
        {
            var user = await _userManager.FindByEmailAsync(userAuthenticationInfo.UserEmail);

//            var checkPasword = await _userManager.CheckPasswordAsync(user, userAuthenticationInfo.Password);
//            if (!checkPasword)
//                throw new ArgumentException();

            var jwtTokenHandler = new JwtSecurityTokenHandler();
            var claims          = MakeClaimsCollection(userAuthenticationInfo);


            var accessToken = (AccessToken)jwtTokenHandler.WriteToken(new JwtSecurityToken(
                                                                          claims: claims,
                                                                          issuer: jwtTokenOptions.Issuer,
                                                                          audience: jwtTokenOptions.Audience,
                                                                          expires: DateTime.Now.Add(TimeSpan.FromSeconds(1000)),
                                                                          signingCredentials: new SigningCredentials(
                                                                              new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenOptions.SecretKey)),
                                                                              SecurityAlgorithms.HmacSha256)
                                                                          ));

            var refreshToken = (RefreshToken)Guid.NewGuid().ToString();

            user.RefreshToken = refreshToken;
            await _authDbContext.SaveChangesAsync(ct);

            return(new AccessRefreshDto(accessToken, refreshToken));
        }
Пример #2
0
        public async Task <AccessRefreshDto> GetJwtToken(
            [FromBody] CustomIdentityUserDto customIdentityUser,
            [FromServices] JwtAuthorizeService jwtAuthorizeService)
        {
            var result = await jwtAuthorizeService.GetJwtToken(customIdentityUser, CancellationToken.None);

            return(result);
        }
Пример #3
0
        public async Task <IActionResult> PrimaryAuthentication(
            [FromBody] CustomIdentityUserDto userAuthenticationInfo,
            [FromServices] IAsyncHandler <CustomIdentityUserWithRolesDto, ConfirmationCodeDto> primarySignupHandler)
        {
            await primarySignupHandler.Handle(
                new CustomIdentityUserWithRolesDto(userAuthenticationInfo.UserName, userAuthenticationInfo.UserEmail,
                                                   userAuthenticationInfo.Password), CancellationToken.None);

            return(Ok());
        }
Пример #4
0
        private IEnumerable <Claim> MakeClaimsCollection(CustomIdentityUserDto user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Email, user.UserEmail),
                new Claim(ClaimTypes.NameIdentifier, user.UserName),
                new Claim("Age", $"{user.Age}")
            };

            return(claims);
        }