Ejemplo n.º 1
0
    public async Task <ActionResult <UserTokenInfoDto> > RefreshAccessTokenAsync([FromBody] UserRefreshTokenDto input)
    {
        var claimOfId = JwtTokenHelper.GetClaimFromRefeshToken(_jwtOptions.Value, input.RefreshToken, JwtRegisteredClaimNames.NameId);

        if (claimOfId is not null)
        {
            var id = claimOfId.Value.ToLong();
            if (id is null)
            {
                return(Forbid());
            }

            var validatedInfo = await _accountService.GetUserValidatedInfoAsync(id.Value);

            if (validatedInfo is null)
            {
                return(Forbid());
            }

            var jti = JwtTokenHelper.GetClaimFromRefeshToken(_jwtOptions.Value, input.RefreshToken, JwtRegisteredClaimNames.Jti);
            if (jti.Value != validatedInfo.ValidationVersion)
            {
                return(Forbid());
            }

            var accessToken  = JwtTokenHelper.CreateAccessToken(_jwtOptions.Value, validatedInfo.ValidationVersion, validatedInfo.Account, validatedInfo.Id.ToString(), validatedInfo.Name, validatedInfo.RoleIds);
            var refreshToken = JwtTokenHelper.CreateRefreshToken(_jwtOptions.Value, validatedInfo.ValidationVersion, validatedInfo.Id.ToString());

            await _accountService.ChangeUserValidateInfoExpiresDtAsync(id.Value);

            var tokenInfo = new UserTokenInfoDto(accessToken.Token, accessToken.Expire, refreshToken.Token, refreshToken.Expire);
            return(Ok(tokenInfo));
        }
        return(Forbid());
    }
Ejemplo n.º 2
0
    public async Task <ActionResult <UserTokenInfoDto> > LoginAsync([FromBody] UserLoginDto input)
    {
        var result = await _accountService.LoginAsync(input);

        if (result.IsSuccess)
        {
            var validatedInfo = result.Content;
            var accessToken   = JwtTokenHelper.CreateAccessToken(_jwtOptions.Value, validatedInfo.ValidationVersion, validatedInfo.Account, validatedInfo.Id.ToString(), validatedInfo.Name, validatedInfo.RoleIds);
            var refreshToken  = JwtTokenHelper.CreateRefreshToken(_jwtOptions.Value, validatedInfo.ValidationVersion, validatedInfo.Id.ToString());
            var tokenInfo     = new UserTokenInfoDto(accessToken.Token, accessToken.Expire, refreshToken.Token, refreshToken.Expire);
            return(Created($"/auth/session", tokenInfo));
        }
        return(Problem(result.ProblemDetails));
    }