As there are hashing differences for identity user passwords we need to keep old users without forcing them to renew their passwords. So I have to change hashing to old style. I am following this answer https://stackoverflow.com/a/57074910/1651298 but new hasher is not being used despite of the fact that PasswordHasher
is replaced in service container.
Steps to reproduce the issue:
Create ASP Core MVC
project for .NET 6
and choose Individual Accounts
for authentication. Change Program.cs
file:
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Proofs.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
var serviceDescriptor = builder.Services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IPasswordHasher<IdentityUser>));
builder.Services.Remove(serviceDescriptor);
builder.Services.AddScoped<IPasswordHasher<IdentityUser>, OldMvcPasswordHasher>();
//builder.Services.Replace(new ServiceDescriptor(
// serviceType: typeof(IPasswordHasher<IdentityUser>),
// implementationType: typeof(OldMvcPasswordHasher),
// ServiceLifetime.Scoped));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
public class OldMvcPasswordHasher : PasswordHasher<IdentityUser>
{
public override PasswordVerificationResult VerifyHashedPassword(IdentityUser user, string hashedPassword, string providedPassword)
{
return PasswordVerificationResult.SuccessRehashNeeded;
}
}
I tried removing and adding new service, also replacing service, but new VerifyHashedPassword
method is not being called when I try to log in.