mirror of https://github.com/abpframework/abp.git
6 changed files with 121 additions and 1 deletions
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Authentication.OpenIdConnect; |
|||
|
|||
public interface IOpenIdLocalUserCreationClient |
|||
{ |
|||
Task CreateOrUpdateAsync(TokenValidatedContext tokenValidatedContext); |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
using System; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
|||
using Microsoft.Extensions.Options; |
|||
using Microsoft.Net.Http.Headers; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Authentication.OpenIdConnect; |
|||
|
|||
public class OpenIdLocalUserCreationClient : IOpenIdLocalUserCreationClient, ITransientDependency |
|||
{ |
|||
protected OpenIdLocalUserCreationClientOptions Options { get; } |
|||
protected IHttpClientFactory HttpClientFactory { get; } |
|||
protected IRemoteServiceConfigurationProvider RemoteServiceConfigurationProvider { get; } |
|||
|
|||
public OpenIdLocalUserCreationClient( |
|||
IOptions<OpenIdLocalUserCreationClientOptions> options, |
|||
IHttpClientFactory httpClientFactory, |
|||
IRemoteServiceConfigurationProvider remoteServiceConfigurationProvider) |
|||
{ |
|||
HttpClientFactory = httpClientFactory; |
|||
RemoteServiceConfigurationProvider = remoteServiceConfigurationProvider; |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual async Task CreateOrUpdateAsync(TokenValidatedContext context) |
|||
{ |
|||
if (!Options.IsEnabled) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
using (var httpClient = HttpClientFactory.CreateClient(Options.HttpClientName)) |
|||
{ |
|||
if (!Options.RemoteServiceName.IsNullOrWhiteSpace()) |
|||
{ |
|||
var configuration = await RemoteServiceConfigurationProvider.GetConfigurationOrDefaultAsync(Options.RemoteServiceName); |
|||
if (configuration.BaseUrl != null) |
|||
{ |
|||
httpClient.BaseAddress = new Uri(configuration.BaseUrl); |
|||
} |
|||
} |
|||
|
|||
httpClient.DefaultRequestHeaders.Add( |
|||
HeaderNames.Authorization, |
|||
"Bearer " + context.SecurityToken.RawData |
|||
); |
|||
|
|||
var response = await httpClient.PostAsync( |
|||
Options.Url, |
|||
new StringContent(string.Empty) |
|||
); |
|||
|
|||
response.EnsureSuccessStatusCode(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
using System.Net.Http; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Authentication.OpenIdConnect; |
|||
|
|||
public class OpenIdLocalUserCreationClientOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Can be used to enable/disable request to the server to create/update local users.
|
|||
/// Default value: false
|
|||
/// </summary>
|
|||
public bool IsEnabled { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Default value: "AbpIdentity".
|
|||
/// Fallbacks to the "Default" remote service configuration, if "AbpIdentity" configuration is not available.
|
|||
/// Set to null if you don't want to use a remote service configuration. In this case, you can set an
|
|||
/// absolute URL in the <see cref="Url"/> option.
|
|||
/// </summary>
|
|||
public string RemoteServiceName { get; set; } = "AbpIdentity"; |
|||
|
|||
/// <summary>
|
|||
/// URL to make a POST request after the current user successfully authenticated through an OpenIdConnect provider.
|
|||
/// </summary>
|
|||
public string Url { get; set; } = "/api/identity-profile/create-or-update"; |
|||
|
|||
/// <summary>
|
|||
/// Can be set to a value if you want to use a named <see cref="HttpClient"/> instance
|
|||
/// while creating it from <see cref="IHttpClientFactory"/>.
|
|||
/// Default value: "" (<see cref="Microsoft.Extensions.Options.Options.DefaultName"/>).
|
|||
/// </summary>
|
|||
public string HttpClientName { get; } = Microsoft.Extensions.Options.Options.DefaultName; |
|||
} |
|||
Loading…
Reference in new issue