Example #1
0
        /// <summary>
        /// Retrieves the current authenticated user. If there is no currently
        /// authenticated user then <c>null</c> is returned.
        /// If <paramref name="autoRefresh"/> is <c>true</c>, then
        /// the access token is automatically refreshed if the refresh token is not expired.
        /// If <paramref name="autoRefresh"/> is <c>true</c>, the access token needs to
        /// be refreshed but the refresh is not possible, <c>null</c> is returned.
        /// </summary>
        /// <returns>The current user if authenticated, <c>null</c> otherwise.</returns>
        /// <param name="autoRefresh">Whether the access token should be silenty refreshed or not.</param>
        public override async Task <User> CurrentUser(bool autoRefresh)
        {
            var serializedCredential = CredentialManager.LoadSerialized();

            if (serializedCredential == null)
            {
                return(null);
            }
            var parsedCredential = new OIDCCredential(serializedCredential);

            if (autoRefresh && parsedCredential.NeedsRenewal)
            {
                try
                {
                    await parsedCredential.Refresh().ConfigureAwait(false);
                }
                catch (Exception)
                {
                    // Credential needs renewal but we have not been able to refresh
                    return(null);
                }

                CredentialManager.Store(parsedCredential);
            }

            User currentUser = User.NewUser().FromUnverifiedCredential(parsedCredential, KeycloakConfig.ResourceId);

            return(currentUser);
        }
Example #2
0
        /// <summary>
        /// Retrieves the current authenticated user. If there is no currently
        /// authenticated user then <c>null</c> is returned.
        /// </summary>
        /// <returns>The current user if authenticated. Else <c>null</c>.</returns>
        public override User CurrentUser()
        {
            var serializedCredential = CredentialManager.LoadSerialized();

            if (serializedCredential == null)
            {
                return(null);
            }
            var parsedCredential = new OIDCCredential(serializedCredential);

            return(User.NewUser().FromUnverifiedCredential(parsedCredential, KeycloakConfig.ResourceId));
        }
        private async Task <User> exchangeTokens(AuthorizationResponse response)
        {
            TokenResponse tokenResponse = await authorizationService.PerformTokenRequestAsync(response.CreateTokenExchangeRequest());

            authState.Update(tokenResponse, null);
            ICredential credential = new OIDCCredential(authState.JsonSerializeString());

            credentialManager.Store(credential);
            User user = GetUser(credential);

            return(user);
        }