Exemplo n.º 1
0
        /// <inheritdoc />
        protected async override Task <ClaimsPrincipal> GetAuthenticatedUser()
        {
            var tokenResult = await RequestAccessToken().ConfigureAwait(false);

            if (tokenResult.TryGetToken(out var accessToken))
            {
                using var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken.Value);

                TAccount account = null;

                if (_currentScopes.Contains("User.Read"))
                {
                    var response = await httpClient.GetAsync(UserInfoEndpoint).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                        account = await JsonSerializer.DeserializeAsync <TAccount>(stream).ConfigureAwait(false);
                    }
                }
                else
                {
                    account = new TAccount();
                }

                AddIdTokenClaimsToAccount(account, _idToken);
                return(await AccountClaimsPrincipalFactory.CreateUserAsync(account, Options.UserOptions).ConfigureAwait(false));
            }

            return(new ClaimsPrincipal(new ClaimsIdentity()));
        }
Exemplo n.º 2
0
    /// <summary>
    /// Gets the current authenticated used using JavaScript interop.
    /// </summary>
    /// <returns>A <see cref="Task{ClaimsPrincipal}"/>that will return the current authenticated user when completes.</returns>
    protected internal virtual async ValueTask <ClaimsPrincipal> GetAuthenticatedUser()
    {
        await EnsureAuthService();

        var account = await JsRuntime.InvokeAsync <TAccount>("AuthenticationService.getUser");

        var user = await AccountClaimsPrincipalFactory.CreateUserAsync(account, Options.UserOptions);

        return(user);
    }