Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize ".NET 8 Identity" routes - MapIdentityApi

I added the new identity endpoint from .NET 8 with .MapIdentityApi(). How can I expand the "/register" and "/login" endpoint to allow the username to be passed? When I register a user the username is set as the email.

I expanded the IdentityUser with additional fields and migrated the classes to my MySQL database. That worked fine. All the endpoints stayed the same.

like image 455
noahkriesi Avatar asked Jan 22 '26 00:01

noahkriesi


1 Answers

This is what I did. I took the source code @ https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs

I copied that and put it in my .NET 8 Api Application. Then I changed the line:

 public static IEndpointConventionBuilder MapIdentityApi<TUser>(this IEndpointRouteBuilder endpoints)

to

public static IEndpointConventionBuilder MapCustomIdentityApi<TUser>(this IEndpointRouteBuilder endpoints)

There are a couple of references to the class name in the code that you'll need to update. Based on what end points you keep. I removed the register, which is one, the other is the confirm email.

Then I added/removed/customized the endpoints in there.

Then in Program.cs I called:

app.MapCustomIdentityApi<IdentityUser>(); 

Hopefully, you can take it from there and save the world!

like image 69
user1579943 Avatar answered Jan 23 '26 20:01

user1579943