2020-08-24 14:23:08 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-07-07 12:39:24 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2020-05-15 15:21:15 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-05-18 08:21:34 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2020-05-17 07:56:59 +01:00
|
|
|
|
using Umbraco.Core;
|
2020-05-17 08:48:36 +01:00
|
|
|
|
using Umbraco.Core.BackOffice;
|
2020-05-27 13:48:26 +10:00
|
|
|
|
using Umbraco.Core.Security;
|
|
|
|
|
|
using Umbraco.Core.Serialization;
|
2020-10-26 10:47:14 +00:00
|
|
|
|
using Umbraco.Infrastructure.BackOffice;
|
2020-05-17 07:56:59 +01:00
|
|
|
|
using Umbraco.Net;
|
2020-07-07 12:39:24 +02:00
|
|
|
|
using Umbraco.Web.BackOffice.Filters;
|
2020-05-21 15:43:33 +10:00
|
|
|
|
using Umbraco.Web.BackOffice.Security;
|
2020-05-17 07:56:59 +01:00
|
|
|
|
using Umbraco.Web.Common.AspNetCore;
|
2020-06-02 13:28:30 +10:00
|
|
|
|
using Umbraco.Web.Common.Security;
|
2020-05-15 15:21:15 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Extensions
|
|
|
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
|
public static class BackOfficeServiceCollectionExtensions
|
|
|
|
|
|
{
|
2020-09-29 12:49:32 +02:00
|
|
|
|
|
2020-05-26 22:21:22 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds the services required for using Umbraco back office Identity
|
|
|
|
|
|
/// </summary>
|
2020-06-05 12:50:26 +02:00
|
|
|
|
/// <param name="services"></param>
|
2020-05-17 10:39:30 +01:00
|
|
|
|
public static void AddUmbracoBackOfficeIdentity(this IServiceCollection services)
|
2020-05-15 15:21:15 +01:00
|
|
|
|
{
|
2020-05-18 08:21:34 +01:00
|
|
|
|
services.AddDataProtection();
|
|
|
|
|
|
|
2020-05-21 15:43:33 +10:00
|
|
|
|
services.BuildUmbracoBackOfficeIdentity()
|
2020-05-15 15:21:15 +01:00
|
|
|
|
.AddDefaultTokenProviders()
|
2020-05-17 07:56:59 +01:00
|
|
|
|
.AddUserStore<BackOfficeUserStore>()
|
2020-09-22 14:44:41 +02:00
|
|
|
|
.AddUserManager<IBackOfficeUserManager, BackOfficeUserManager>()
|
2020-05-27 13:48:26 +10:00
|
|
|
|
.AddSignInManager<BackOfficeSignInManager>()
|
2020-05-18 08:21:34 +01:00
|
|
|
|
.AddClaimsPrincipalFactory<BackOfficeClaimsPrincipalFactory<BackOfficeIdentityUser>>();
|
|
|
|
|
|
|
2020-05-21 15:43:33 +10:00
|
|
|
|
// Configure the options specifically for the UmbracoBackOfficeIdentityOptions instance
|
2020-06-02 13:28:30 +10:00
|
|
|
|
services.ConfigureOptions<ConfigureBackOfficeIdentityOptions>();
|
|
|
|
|
|
services.ConfigureOptions<ConfigureBackOfficeSecurityStampValidatorOptions>();
|
2020-09-02 18:10:29 +10:00
|
|
|
|
}
|
2020-05-20 15:25:42 +10:00
|
|
|
|
|
2020-10-26 10:47:14 +00:00
|
|
|
|
private static BackOfficeIdentityBuilder BuildUmbracoBackOfficeIdentity(this IServiceCollection services)
|
2020-05-20 15:25:42 +10:00
|
|
|
|
{
|
2020-05-21 15:43:33 +10:00
|
|
|
|
// Borrowed from https://github.com/dotnet/aspnetcore/blob/master/src/Identity/Extensions.Core/src/IdentityServiceCollectionExtensions.cs#L33
|
|
|
|
|
|
// The reason we need our own is because the Identity system doesn't cater easily for multiple identity systems and particularly being
|
|
|
|
|
|
// able to configure IdentityOptions to a specific provider since there is no named options. So we have strongly typed options
|
2020-06-05 12:50:26 +02:00
|
|
|
|
// and strongly typed ILookupNormalizer and IdentityErrorDescriber since those are 'global' and we need to be unintrusive.
|
2020-05-21 15:43:33 +10:00
|
|
|
|
|
2020-05-27 13:48:26 +10:00
|
|
|
|
// TODO: Could move all of this to BackOfficeComposer?
|
|
|
|
|
|
|
2020-05-21 15:43:33 +10:00
|
|
|
|
// Services used by identity
|
|
|
|
|
|
services.TryAddScoped<IUserValidator<BackOfficeIdentityUser>, UserValidator<BackOfficeIdentityUser>>();
|
|
|
|
|
|
services.TryAddScoped<IPasswordValidator<BackOfficeIdentityUser>, PasswordValidator<BackOfficeIdentityUser>>();
|
2020-05-27 13:48:26 +10:00
|
|
|
|
services.TryAddScoped<IPasswordHasher<BackOfficeIdentityUser>>(
|
|
|
|
|
|
services => new BackOfficePasswordHasher(
|
2020-10-07 16:56:48 +11:00
|
|
|
|
new LegacyPasswordSecurity(),
|
2020-05-27 13:48:26 +10:00
|
|
|
|
services.GetRequiredService<IJsonSerializer>()));
|
2020-05-21 15:43:33 +10:00
|
|
|
|
services.TryAddScoped<IUserConfirmation<BackOfficeIdentityUser>, DefaultUserConfirmation<BackOfficeIdentityUser>>();
|
|
|
|
|
|
services.TryAddScoped<IUserClaimsPrincipalFactory<BackOfficeIdentityUser>, UserClaimsPrincipalFactory<BackOfficeIdentityUser>>();
|
2020-11-17 07:56:04 +00:00
|
|
|
|
|
2020-06-05 12:50:26 +02:00
|
|
|
|
// CUSTOM:
|
2020-05-21 15:43:33 +10:00
|
|
|
|
services.TryAddScoped<BackOfficeLookupNormalizer>();
|
|
|
|
|
|
services.TryAddScoped<BackOfficeIdentityErrorDescriber>();
|
2020-10-23 14:57:35 +11:00
|
|
|
|
services.TryAddScoped<IIpResolver, AspNetCoreIpResolver>();
|
2020-11-27 13:33:01 +01:00
|
|
|
|
services.TryAddSingleton<IBackOfficeExternalLoginProviders, BackOfficeExternalLoginProviders>();
|
2020-05-21 15:43:33 +10:00
|
|
|
|
|
2020-11-17 07:56:04 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* IdentityBuilderExtensions.AddUserManager adds UserManager<BackOfficeIdentityUser> to service collection
|
|
|
|
|
|
* To validate the container the following registrations are required (dependencies of UserManager<T>)
|
|
|
|
|
|
* Perhaps we shouldn't be registering UserManager<T> at all and only registering/depending the UmbracoBackOffice prefixed types.
|
|
|
|
|
|
*/
|
|
|
|
|
|
services.TryAddScoped<ILookupNormalizer, BackOfficeLookupNormalizer>();
|
|
|
|
|
|
services.TryAddScoped<IdentityErrorDescriber, BackOfficeIdentityErrorDescriber>();
|
|
|
|
|
|
|
2020-10-26 10:47:14 +00:00
|
|
|
|
return new BackOfficeIdentityBuilder(services);
|
2020-05-20 15:25:42 +10:00
|
|
|
|
}
|
2020-05-15 15:21:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|