protected virtual async Task <string> GetAccessTokenFromServerOrNullAsync(IdentityModelHttpClientAuthenticateContext context) { //TODO: Better logging var configuration = GetClientConfiguration(context); if (configuration == null) { Logger.LogWarning($"Could not find {nameof(IdentityClientConfiguration)} for {context.IdentityClientName}. Either define a configuration for {context.IdentityClientName} or set a default configuration."); return(null); } var discoveryResponse = await GetDiscoveryResponse(configuration); if (discoveryResponse.IsError) { Logger.LogError($"Could not retrieve the OpenId Connect discovery document! ErrorType: {discoveryResponse.ErrorType}. Error: {discoveryResponse.Error}"); return(null); } var tokenResponse = await GetTokenResponse(discoveryResponse, configuration); if (tokenResponse.IsError) { Logger.LogError($"Could not get token from the OpenId Connect server! ErrorType: {tokenResponse.ErrorType}. Error: {tokenResponse.Error}. ErrorDescription: {tokenResponse.ErrorDescription}. HttpStatusCode: {tokenResponse.HttpStatusCode}"); return(null); } return(tokenResponse.AccessToken); }
protected virtual async Task <string> GetAccessTokenFromServerOrNullAsync(IdentityModelHttpClientAuthenticateContext context) { var configuration = GetClientConfiguration(context); if (configuration == null) { return(null); } var discoveryResponse = await GetDiscoveryResponse(configuration); if (discoveryResponse.IsError) { return(null); } var tokenResponse = await GetTokenResponse(discoveryResponse, configuration); if (tokenResponse.IsError) { return(null); } return(tokenResponse.AccessToken); }
private IdentityClientConfiguration GetClientConfiguration(IdentityModelHttpClientAuthenticateContext context) { if (context.IdentityClientName.IsNullOrEmpty()) { return(ClientOptions.IdentityClients.Default); } return(ClientOptions.IdentityClients.GetOrDefault(context.IdentityClientName) ?? ClientOptions.IdentityClients.Default); }
public async Task Authenticate(IdentityModelHttpClientAuthenticateContext context) { var accessToken = await GetAccessTokenFromServerOrNullAsync(context); if (accessToken != null) { //TODO: "Bearer" should be configurable context.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } }