Ejemplo n.º 1
0
        /// <inheritdoc/>
        public async Task RequestTokenWithExternalProviderAsync(ExternalLoginRequest externalLoginRequest)
        {
            if (!this.IsAuthenticated)
            {
                this.authenticationResult = await this.authenticationServiceAgent.RequestTokenWithExternalProviderAsync(externalLoginRequest);

                await this.LoadUserTokensAsync();
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public async Task RequestTokenAsync(LoginRequest loginRequest)
        {
            if (!this.IsAuthenticated)
            {
                this.authenticationResult = await this.authenticationServiceAgent.RequestTokenAsync(loginRequest);

                await this.LoadUserTokensAsync();
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task <BearerAuthenticationResult> BuildJwtTokenForUserAsync(Guid userId)
        {
            try
            {
                var user = await this.userManager.FindByIdAsync(userId.ToString());

                var userClaims = await this.userClaimsService.GetUserClaimsForJwtTokenAsync(userId);

                string jwt          = this.BuildJwtToken(userClaims);
                string refreshToken = this.context.BuildRefreshToken(user);
                await this.context.SaveChangesAsync();

                return(BearerAuthenticationResult.SuccessResult(jwt, refreshToken));
            }
            catch (Exception ex)
            {
                await this.logger.LogErrorAsync(ex);

                return(null);
            }
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public async Task <BearerAuthenticationResult> RefreshJwtTokenAsync(Guid?userId, string refreshToken)
        {
            User user = null;

            if (userId.HasValue)
            {
                user = await this.userManager.FindByIdAsync(userId.ToString());
            }
            else
            {
                user = await this.context.Set <User>().AsQueryable().FirstOrDefaultAsync(x => x.RefreshToken == refreshToken);
            }

            if (user != null && user.RefreshToken == refreshToken && user.RefreshTokenExpiration.HasValue && user.RefreshTokenExpiration > DateTime.Now)
            {
                var userClaims = await this.userClaimsService.GetUserClaimsForJwtTokenAsync(user.Id);

                string jwt = this.BuildJwtToken(userClaims);

                return(BearerAuthenticationResult.SuccessResult(jwt, refreshToken));
            }

            return(BearerAuthenticationResult.FailedResult);
        }