2020-05-17 07:56:59 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Security.Claims;
|
2020-05-15 16:52:51 +01:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
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-20 15:25:42 +10:00
|
|
|
|
using Microsoft.Extensions.Options;
|
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-20 15:25:42 +10:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-05-17 07:56:59 +01:00
|
|
|
|
using Umbraco.Net;
|
|
|
|
|
|
using Umbraco.Web.Common.AspNetCore;
|
2020-05-15 15:21:15 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Extensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class UmbracoBackOfficeServiceCollectionExtensions
|
|
|
|
|
|
{
|
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-18 13:00:32 +01:00
|
|
|
|
services.TryAddScoped<IIpResolver, AspNetCoreIpResolver>();
|
2020-05-17 07:56:59 +01:00
|
|
|
|
|
2020-05-20 15:25:42 +10:00
|
|
|
|
services
|
|
|
|
|
|
.AddIdentityCore<BackOfficeIdentityUser>()
|
2020-05-15 15:21:15 +01:00
|
|
|
|
.AddDefaultTokenProviders()
|
2020-05-17 07:56:59 +01:00
|
|
|
|
.AddUserStore<BackOfficeUserStore>()
|
2020-05-18 08:21:34 +01:00
|
|
|
|
.AddUserManager<BackOfficeUserManager>()
|
|
|
|
|
|
.AddClaimsPrincipalFactory<BackOfficeClaimsPrincipalFactory<BackOfficeIdentityUser>>();
|
|
|
|
|
|
|
2020-05-20 15:25:42 +10:00
|
|
|
|
services.ConfigureOptions<UmbracoBackOfficeIdentityOptions>();
|
2020-05-18 08:21:34 +01:00
|
|
|
|
services.AddScoped<ILookupNormalizer, NopLookupNormalizer>();
|
|
|
|
|
|
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<BackOfficeIdentityUser>>();
|
2020-05-15 15:21:15 +01:00
|
|
|
|
|
|
|
|
|
|
}
|
2020-05-20 15:25:42 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to configure <see cref="IdentityOptions"/> for the Umbraco Back office
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class UmbracoBackOfficeIdentityOptions : IConfigureOptions<IdentityOptions>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserPasswordConfiguration _userPasswordConfiguration;
|
|
|
|
|
|
|
|
|
|
|
|
public UmbracoBackOfficeIdentityOptions(IUserPasswordConfiguration userPasswordConfiguration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_userPasswordConfiguration = userPasswordConfiguration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Configure(IdentityOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.User.RequireUniqueEmail = true;
|
|
|
|
|
|
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier;
|
|
|
|
|
|
options.ClaimsIdentity.UserNameClaimType = ClaimTypes.Name;
|
|
|
|
|
|
options.ClaimsIdentity.RoleClaimType = ClaimTypes.Role;
|
|
|
|
|
|
options.ClaimsIdentity.SecurityStampClaimType = Constants.Web.SecurityStampClaimType;
|
|
|
|
|
|
options.Lockout.AllowedForNewUsers = true;
|
|
|
|
|
|
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromDays(30);
|
|
|
|
|
|
|
|
|
|
|
|
options.Password.RequiredLength = _userPasswordConfiguration.RequiredLength;
|
|
|
|
|
|
options.Password.RequireNonAlphanumeric = _userPasswordConfiguration.RequireNonLetterOrDigit;
|
|
|
|
|
|
options.Password.RequireDigit = _userPasswordConfiguration.RequireDigit;
|
|
|
|
|
|
options.Password.RequireLowercase = _userPasswordConfiguration.RequireLowercase;
|
|
|
|
|
|
options.Password.RequireUppercase = _userPasswordConfiguration.RequireUppercase;
|
|
|
|
|
|
options.Lockout.MaxFailedAccessAttempts = _userPasswordConfiguration.MaxFailedAccessAttemptsBeforeLockout;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-15 15:21:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|