I am using the new Identity API endpoints from my ASP.NET Core API project, but I would like to use more custom properties for application user.
My user class looks like this:
public class ApplicationUser : IdentityUser
{
[StringLength(256)]
public string? Name { get; set; }
[StringLength(256)]
public string? Surname { get; set; }
/// <summary>
/// Years and fraction of year of experience on drums
/// </summary>
public float Experience { get; set; }
}
Identity configuration:
public static IServiceCollection ConfigureIdentity(this IServiceCollection services, IConfiguration configuration)
{
var connectionString= configuration.GetConnectionString("IdentityConnection");
//Guard.Against.Null(connectionString, $"Connection string for 'IdentityConnection' not found");
services.AddDbContext<AppIdentityDbContext>((sp, options) =>
{
options.UseSqlite("DataSource=app.db");
});
services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<AppIdentityDbContext>());
services.AddScoped<AppIdentityDbContextInitializer>();
services.AddIdentityApiEndpoints<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>();
services.AddAuthorizationBuilder();
services.AddTransient<IIdentityService, IdentityService>();
services.AddAuthorizationBuilder()
.AddPolicy(Policies.CanAddHomework, policy => policy.RequireRole(Roles.Teacher));
return services;
}
And in API Program class: app.MapIdentityApi<ApplicationUser>();
Database has been created with custom properties, but while using the register endpoint, I am only able to pass only email and password, no matter what I’ve sent in the request body. I can’t even set the username which is the default property in the IdentityUser class. Always the username is email.
Is there an option to configure the register endpoint to accept a custom body or do I need to get rid of the Identity API endpoints and implement authentication with the old Identity?
2
Answers
The Identity API endpoints provided by ASP.NET Core Identity are designed to be flexible and allow you to edit stuff, but they do have certain constraints, especially when it comes to the signup process. By default, the registration endpoint expects a username and password, and this behavior is deeply ingrained in the way the identity system works. You might need to implement your own registration logic to work on custom properties. Here’s a basic outline of how you could approach this:
Create a class that represents the data you want to accept in the
registration request.
Create a new endpoint or modify the existing one to handle the
registration logic.
Ensure that your application’s services are configured correctly.
You may need to modify the registration process in your Startup.cs
file.
If you’re using a client application to register users, update the
registration request to include the additional properties.
Thats a basic example I can imagine that can help you over.
i have the same problem but i think still need to make custom endpoints for it