I’m trying to learn .NET8 and I’m building my first API. In my project I’m using Identity.EntityFrameworkCore to be able to handle user related stuff.
I created my custom User class in order to add a few properties for the user, here is my class.
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
namespace Blog.Models.Entities
{
public class User : IdentityUser
{
[DataType(DataType.DateTime)]
public DateTime? BirthDate { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public byte[]? ProfilePicture { get; set; }
public virtual ICollection<Article>? blogs { get; set; }
}
}
I did the required configuration and I got access to the default endpoints /login, /register… which is perfect, but when I try to register a user, I can only provide the email and passowrd in my request body but I want to be able to provide the other properties I created in my custom User class and to do that I created my custom register endpoint
[HttpPost("register")]
public async Task<IActionResult> Register(RegisterUserDto input)
{
var user = new User()
{
FirstName = input.FirstName,
LastName = input.LastName,
BirthDate = input.BirthDate,
Email = input.Email,
UserName = input.UserName,
};
var result = await _userManager.CreateAsync(user, input.PasswordHash);
string message = string.Empty;
if (result.Succeeded)
return Ok("User created successfully");
return BadRequest("Error occured");
}
And I got the expected result. However my problem is that now, I do have 2 register endpoints, the one provided by Identity and the one I created. I can remove
builder.Services.AddIdentityCore<User>()
.AddEntityFrameworkStores<AppDbContext>()
.AddApiEndpoints();
and
app.MapIdentityApi<User>();
from Program.cs,
but that will also get rid of the other endpoints such as /login, /refresh, /forgotPassword… which I want to keep.
And so my question is, is it possible to only get rid of the default register method while keeping the others?
1
Unfortunately, it is not possible to override/edit the built-in .NET Identity API endpoints.
Although they are great for default implementations of Web API security using the Identity Framework they are not extensible.
I found this answer to another question, it explains exactly what you need to implement your own endpoints.
Hope this helps!