Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Last active July 27, 2021 15:13
Show Gist options
  • Save warrenbuckley/657352898df471743715fed721610ddb to your computer and use it in GitHub Desktop.
Save warrenbuckley/657352898df471743715fed721610ddb to your computer and use it in GitHub Desktop.
.NET Core Identity PasswordValidators with Umbraco CMS BackOffice Users
<!-- Place User override lang files at /config/lang -->
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<language alias="en_us" intName="English (US)" localName="English (US)" lcid="" culture="en-US">
<creator>
<name>Warren Buckley</name>
<link>https://warrenbuckley.co.uk/</link>
</creator>
<area alias="validation">
<key alias="passwordTooEasy">You cannot use the word 'password' as part of your password</key>
</area>
</language>
using Microsoft.AspNetCore.Identity;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.UI.NetCore
{
public class MyCustomIdentityErrorDescriber : BackOfficeErrorDescriber
{
private ILocalizedTextService _textService;
public MyCustomIdentityErrorDescriber(ILocalizedTextService textService) : base(textService)
{
_textService = textService;
}
public IdentityError PasswordTooEasy()
{
return new IdentityError
{
Code = "SayNoToEasyPasswords",
Description = _textService.Localize("validation", "passwordTooEasy")
};
}
}
}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Umbraco.Cms.Core.Security;
namespace Umbraco.Cms.Web.UI.NetCore
{
public class SayNoToEasyPasswordsValidator : IPasswordValidator<BackOfficeIdentityUser>
{
private MyCustomIdentityErrorDescriber _errors;
public SayNoToEasyPasswordsValidator(MyCustomIdentityErrorDescriber errors)
{
_errors = errors;
}
public Task<IdentityResult> ValidateAsync(UserManager<BackOfficeIdentityUser> manager, BackOfficeIdentityUser user, string password)
{
if (password.ToLowerInvariant().Contains("password"))
{
return Task.FromResult(IdentityResult.Failed(_errors.PasswordTooEasy()));
}
return Task.FromResult(IdentityResult.Success);
}
}
}
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Security;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.UI.NetCore
{
public class Startup
{
private readonly IWebHostEnvironment _env;
private readonly IConfiguration _config;
/// <summary>
/// Initializes a new instance of the <see cref="Startup"/> class.
/// </summary>
/// <param name="webHostEnvironment">The Web Host Environment</param>
/// <param name="config">The Configuration</param>
/// <remarks>
/// Only a few services are possible to be injected here https://github.com/dotnet/aspnetcore/issues/9337
/// </remarks>
public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config)
{
_env = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
_config = config ?? throw new ArgumentNullException(nameof(config));
}
/// <summary>
/// Configures the services
/// </summary>
/// <remarks>
/// This method gets called by the runtime. Use this method to add services to the container.
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
/// </remarks>
public void ConfigureServices(IServiceCollection services)
{
#pragma warning disable IDE0022 // Use expression body for methods
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.Build();
#pragma warning restore IDE0022 // Use expression body for methods
var backofficeIdentityBuilder = new BackOfficeIdentityBuilder(services);
backofficeIdentityBuilder.AddPasswordValidator<SayNoToEasyPasswordsValidator>();
backofficeIdentityBuilder.AddErrorDescriber<MyCustomIdentityErrorDescriber>();
}
/// <summary>
/// Configures the application
/// </summary>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseUmbraco()
.WithMiddleware(u =>
{
u.WithBackOffice();
u.WithWebsite();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment