public async Task UpdateAsync(IAuthenticateResult authenticateResult, CancellationToken cancellationToken) { var httpContext = this.httpContextAccessor.HttpContext; await httpContext.SignOutAsync(this.authenticationScheme).ConfigureAwait(false); await httpContext.SignInAsync(this.authenticationScheme, authenticateResult.Principal, authenticateResult.Properties).ConfigureAwait(false); }
public AuthenticationTicket(IAuthenticateResult authenticateResult) { if (!SpotifyRequiredTokensUtils.TryGet(authenticateResult.Properties, out var tokens)) { throw new SpotifyMalformedAuthenticationTicketException("Unable to get authentication tokens."); } this.RefreshToken = tokens.Value.RefreshToken; this.AccessToken = new AccessToken( tokens.Value.AccessToken, tokens.Value.ExpiresAt); this.User = new PrincipalUser(authenticateResult.Principal); }
public void SetAuthenticateResult(string userId, string refreshToken, string accessToken, DateTimeOffset expiresAt) { var principal = new ClaimsPrincipal(); principal.AddIdentity(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, userId) })); var properties = new AuthenticationProperties(new Dictionary <string, string>() { [".Token.refresh_token"] = refreshToken, [".Token.access_token"] = accessToken, [".Token.expires_at"] = expiresAt.ToString("o", CultureInfo.InvariantCulture) }); this.authenticateResult = new AuthenticateResult { Principal = principal, Properties = properties }; }
public AuthenticationTicket(IAuthenticateResult authenticateResult) { var refreshToken = authenticateResult.Properties.GetTokenValue(RefreshTokenKey); var accessToken = authenticateResult.Properties.GetTokenValue(AccessTokenKey); var expiresAt = authenticateResult.Properties.GetTokenValue(ExpiresAtKey); if (string.IsNullOrEmpty(refreshToken) || string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(expiresAt)) { throw new InvalidOperationException("Authentication tokens were not found."); } this.RefreshToken = refreshToken; this.AccessToken = new AccessToken( accessToken, DateTimeOffset.ParseExact(expiresAt, "o", CultureInfo.InvariantCulture)); this.User = new PrincipalUser(authenticateResult.Principal); }
private async Task <AuthenticationTicket> GetNewAccessTokenAsync(AuthenticationTicket authenticationTicket, IAuthenticateResult authenticateResult, CancellationToken cancellationToken) { var accessTokenResponse = await this.tokenClient.GetAccessTokenFromRefreshTokenAsync(authenticationTicket.RefreshToken, cancellationToken).ConfigureAwait(false); var accessToken = accessTokenResponse.GetAccessTokenModel(this.clock); authenticateResult.Properties.UpdateTokenValue(TokenNames.AccessToken, accessToken.Token); authenticateResult.Properties.UpdateTokenValue(TokenNames.ExpiresAt, accessToken.ExpiresAt.ToString("o", CultureInfo.InvariantCulture)); await this.authenticationManager.UpdateAsync(authenticateResult, cancellationToken).ConfigureAwait(false); return(new AuthenticationTicket(authenticateResult)); }
Task IAuthenticationManager.UpdateAsync(IAuthenticateResult authenticateResult, CancellationToken cancellationToken) { this.authenticateResult = authenticateResult; return(Task.CompletedTask); }
private async Task <AuthenticationTicket> GetNewAccessTokenAsync(AuthenticationTicket authenticationTicket, IAuthenticateResult authenticateResult, CancellationToken cancellationToken) { var accessTokenDto = await this.tokenHttpClient .GetAccessTokenOrThrowInvalidRefreshTokenExceptionAsync(authenticationTicket.RefreshToken, cancellationToken) .ConfigureAwait(false); var accessToken = accessTokenDto.ToModelToken(this.dateTimeOffsetProvider); authenticateResult.Properties.UpdateTokenValue(AccessTokenKey, accessToken.Token); authenticateResult.Properties.UpdateTokenValue(ExpiresAtKey, accessToken.ExpiresAt.ToString("o", CultureInfo.InvariantCulture)); await this.authenticationManager.UpdateAsync(authenticateResult, cancellationToken).ConfigureAwait(false); return(new AuthenticationTicket(authenticateResult)); }