Browse Source

Introduced OpenIdLocalUserCreationClient and used to create/update local user.

pull/12692/head
Halil İbrahim Kalkan 4 years ago
parent
commit
8811ff7ca0
  1. 16
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs
  2. 1
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj
  3. 5
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/AbpAspNetCoreAuthenticationOpenIdConnectModule.cs
  4. 9
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/IOpenIdLocalUserCreationClient.cs
  5. 59
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/OpenIdLocalUserCreationClient.cs
  6. 32
      framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/OpenIdLocalUserCreationClientOptions.cs

16
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Microsoft/Extensions/DependencyInjection/AbpOpenIdConnectExtensions.cs

@ -3,8 +3,10 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Volo.Abp.AspNetCore.Authentication.OpenIdConnect;
using Volo.Abp.AspNetCore.MultiTenancy;
namespace Microsoft.Extensions.DependencyInjection;
@ -47,6 +49,20 @@ public static class AbpOpenIdConnectExtensions
}
return Task.CompletedTask;
};
options.Events.OnTokenValidated = async (context) =>
{
var client = context.HttpContext.RequestServices.GetRequiredService<IOpenIdLocalUserCreationClient>();
try
{
await client.CreateOrUpdateAsync(context);
}
catch (Exception ex)
{
var logger = context.HttpContext.RequestServices.GetService<ILogger<AbpAspNetCoreAuthenticationOpenIdConnectModule>>();
logger?.LogException(ex);
}
};
});
}

1
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj

@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.AspNetCore.MultiTenancy\Volo.Abp.AspNetCore.MultiTenancy.csproj" />
<ProjectReference Include="..\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj" />
<ProjectReference Include="..\Volo.Abp.RemoteServices\Volo.Abp.RemoteServices.csproj" />
</ItemGroup>
</Project>

5
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/AbpAspNetCoreAuthenticationOpenIdConnectModule.cs

@ -1,12 +1,15 @@
using Volo.Abp.AspNetCore.Authentication.OAuth;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.RemoteServices;
namespace Volo.Abp.AspNetCore.Authentication.OpenIdConnect;
[DependsOn(
typeof(AbpMultiTenancyModule),
typeof(AbpAspNetCoreAuthenticationOAuthModule))]
typeof(AbpAspNetCoreAuthenticationOAuthModule),
typeof(AbpRemoteServicesModule)
)]
public class AbpAspNetCoreAuthenticationOpenIdConnectModule : AbpModule
{

9
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/IOpenIdLocalUserCreationClient.cs

@ -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);
}

59
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/OpenIdLocalUserCreationClient.cs

@ -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();
}
}
}

32
framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo/Abp/AspNetCore/Authentication/OpenIdConnect/OpenIdLocalUserCreationClientOptions.cs

@ -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…
Cancel
Save