How to disable ASP.NET Core Identity auto generated routes?
TL;DR link to the GitHub repo
There is no point denying that ASP.NET Core Identity is a powerful tool. What inconveniences me is the MapIdentityApi method, which is used to add the Endpoints but cannot filter out unnecessary Endpoints, most notably Register Endpoint.
The reason for this is the Microsoft.AspNetCore.Builder.WebApplication.WebApplicationDebugView(app).Endpoints property, which is an IReadOnlyList and holds all of our registered Endpoints. Therefore, we are out of luck trying to change it.
Fortunately for us, we can do something about it. Here is an implementation of IdentityApiEndpointRouteBuilderExtensions class, which implements MapIdentityApi with ASP.NET Core Identity Endpoints. We can extract that class, modify it, and use it in our projects.
First step is to add a new DTO with options to filter out unnecessary routes.
This DTO contains simple boolean properties that will be used to exclude Endpoints.
Second step i to copy this class and paste it into our project. To better describe our new method, rename the MapIdentityApi method to MapIdentityApiFilterable and add a new IdentityApiEndpointRouteBuilderOptions as a parameter.
Third step is to use DTO’s properties for appropriate Endpoint control.
Finally, use our new MapIdentityApiFilterable in the Program.cs file
After running the app, we can see that the excluded Endpoints are missing.
The code can be found on my GitHub repository: https://github.com/darkosubic/EasyAuth
I hope you found this tutorial helpful :)
See you soon with more useful tutorials!