[Identity] AddApiEndpoints() breaks custom SignInManager<TUser>.
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
Call `AddApiEndpoints()` after `AddSignInManager` cause custom SignInManager replaced by default SignInManager.
### Expected Behavior
_No response_
### Steps To Reproduce
``` CSharp
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace SignInManagerReplacement
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext(options =>
{
options.UseInMemoryDatabase("InMemoryDb");
});
builder.Services.AddHttpContextAccessor(); // Add this line to register IHttpContextAccessor
builder.Services.AddAuthentication();
var identityBuilder = builder.Services.AddIdentityCore(options =>
{
// Configure identity options here
options.User.RequireUniqueEmail = false;
})
.AddEntityFrameworkStores()
//.AddApiEndpoints() // <-- call before AddSignInManager works fine
.AddSignInManager()
.AddUserManager>()
.AddDefaultTokenProviders()
.AddApiEndpoints() // <-- call this after AddSignInManager... breaks custom configuration.
;
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// use identity api endpoints
app.MapIdentityApi();
using (var scope = app.Services.CreateScope())
{
// aquire the SignInManager from the DI container
var signInManager = scope.ServiceProvider
.GetRequiredService>();
// !!!!!!!check the type of the SignInManager expected to be `CustomSignInManager`
Console.WriteLine($"Resolved SignInManager type: {signInManager.GetType().FullName}");
}
app.Run();
}
#region Identity sample
internal class IdentityDbContext : IdentityDbContext
{
public IdentityDbContext(DbContextOptions options)
: base(options)
{
}
}
// custom SignInManager
internal class CustomSignInManager: SignInManager
{
public CustomSignInManager(
UserManager userManager,
IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory claimsFactory,
IOptions optionsAccessor,
ILogger> logger,
IAuthenticationSchemeProvider schemes,
IUserConfirmation confirmation)
: base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation)
{
}
}
#endregion
}
}
```
### Exceptions (if any)
_No response_
### .NET Version
8.0
### Anything else?
`AddApiEndpoints()` call `builder.AddSignInManager();` which will breaks the custom configuration.
Contributor guide
Assessment
This issue has not been assessed yet.