C
C#ā€¢3mo ago
Salman

Identity Auth Customization in Asp.Net Core 8 Web API

Hi there! I'm using IdentityAPIEndpoints in my Web API. That includes many endpoints like register/login and others like for confirmation of email etc or 2fa and handles the whole authentication and authorization of the API. But now I wanna implement the RBAC(Role Base Acsess) in it. What I have done so far is that I'm manually creating a admin user by default and assigning him the admin role . But after that now I want to assign every user that registers via the register endpoint provided by the identity, to assign a default User role. So far I've unabled to find the solution like how to assign the roles on registration as the registeration and the whole auth process is being handled by the identity and I've no access to the inside code. Below is my Program.cs file :
1 Reply
Salman
Salmanā€¢3mo ago
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<AppContext>
(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAuthorization();
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<AppContext>();

builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}); ;
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
if (!await roleManager.RoleExistsAsync(Roles.Admin))
{
await roleManager.CreateAsync(new IdentityRole(Roles.Admin));
}
if (!await roleManager.RoleExistsAsync(Roles.User))
{
await roleManager.CreateAsync(new IdentityRole(Roles.User));
}
}
using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
var email = "ecomadmin@gmail.com";
var pass = "Abc@12345";

var user = new IdentityUser();
user.UserName = email;
user.Email = email;

await userManager.CreateAsync(user, pass);
await userManager.AddToRoleAsync(user, Roles.Admin);
}

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapIdentityApi<IdentityUser>();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

public static class Roles
{
public const string Admin = "Admin";
public const string User = "User";
}
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<AppContext>
(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAuthorization();
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<AppContext>();

builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}); ;
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
if (!await roleManager.RoleExistsAsync(Roles.Admin))
{
await roleManager.CreateAsync(new IdentityRole(Roles.Admin));
}
if (!await roleManager.RoleExistsAsync(Roles.User))
{
await roleManager.CreateAsync(new IdentityRole(Roles.User));
}
}
using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
var email = "ecomadmin@gmail.com";
var pass = "Abc@12345";

var user = new IdentityUser();
user.UserName = email;
user.Email = email;

await userManager.CreateAsync(user, pass);
await userManager.AddToRoleAsync(user, Roles.Admin);
}

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapIdentityApi<IdentityUser>();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

public static class Roles
{
public const string Admin = "Admin";
public const string User = "User";
}
How can I reach to the registration code behind the Identity's given endpoint to assign the default role to each user that registers ? Solved So what I did is that , I just created a custom register endpoint on top of the Identity endpoints by leveraging the user manager provided by the identity. So I'll register the users with this endpoint including my custom role assigning logic while the rest of the auth will be done by the other identity endpoints. šŸ˜