public async Task <OidcUserSession> HandleRefreshTokenAsync(OidcUserSession userSession) { if (!userSession.RefreshToken.IsNullOrEmpty() && userSession.ValidUntil < DateTimeOffset.UtcNow.AddSeconds(globalOpenidClientPkceSettings.TokensExpiresBefore)) { var subject = userSession.Claims.Where(c => c.Type == globalOpenidClientPkceSettings.NameClaimType).Select(c => c.Value).SingleOrDefault(); (var idTokenPrincipal, var tokenResponse) = await AcquireRefreshTokensAsync(userSession.OidcDiscoveryUri, userSession.ClientId, subject, userSession.RefreshToken); var validUntil = DateTimeOffset.UtcNow.AddSeconds(tokenResponse.ExpiresIn.HasValue ? tokenResponse.ExpiresIn.Value : 0).AddSeconds(-globalOpenidClientPkceSettings.TokensExpiresBefore); return(await(authenticationStateProvider as OidcAuthenticationStateProvider).UpdateSessionAsync(validUntil, idTokenPrincipal, tokenResponse, userSession.SessionState, userSession)); } return(userSession); }
public async Task CreateSessionAsync(DateTimeOffset validUntil, ClaimsPrincipal claimsPrincipal, string idToken, string accessToken, string sessionState) { var claimsIdentity = claimsPrincipal.Identities.First(); var userSession = new OidcUserSession { ValidUntil = validUntil, Claims = claimsIdentity.Claims.Select(c => new KeyValuePair <string, string>(c.Type, c.Value)), AuthenticationType = claimsIdentity.AuthenticationType, IdToken = idToken, AccessToken = accessToken, SessionState = sessionState }; await sessionStorage.SetItemAsync(userSessionKey, userSession); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); }
private async Task <OidcUserSession> CreateUpdateSessionAsync(DateTimeOffset validUntil, ClaimsPrincipal claimsPrincipal, TokenResponse tokenResponse, string sessionState, string oidcDiscoveryUri, string clientId) { var claimsIdentity = claimsPrincipal.Identities.First(); var claimsList = claimsIdentity.Claims.Select(c => new ClaimValue { Type = c.Type, Value = c.Value }).ToList(); var userSession = new OidcUserSession { ValidUntil = validUntil, Claims = claimsList, AuthenticationType = claimsIdentity.AuthenticationType, IdToken = tokenResponse.IdToken, AccessToken = tokenResponse.AccessToken, RefreshToken = tokenResponse.RefreshToken, SessionState = sessionState, OidcDiscoveryUri = oidcDiscoveryUri, ClientId = clientId }; await sessionStorage.SetItemAsync(userSessionKey, userSession); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); return(userSession); }
public Task <OidcUserSession> UpdateSessionAsync(DateTimeOffset validUntil, ClaimsPrincipal claimsPrincipal, TokenResponse tokenResponse, string sessionState, OidcUserSession userSession) { return(CreateUpdateSessionAsync(validUntil, claimsPrincipal, tokenResponse, sessionState, userSession.OidcDiscoveryUri, userSession.ClientId)); }