ВВассралман
ВВассралман
CC#
Created by ВВассралман on 2/20/2025 in #help
Ef core seed (data)
namespace EventManager.SqlRepository.EntityConfigurations
{
internal sealed class ApplicationUserRolesConfiguration : IEntityTypeConfiguration<ApplicationUserRole>
{
public void Configure(EntityTypeBuilder<ApplicationUserRole> builder)
{
builder.HasData(
new ApplicationUserRole
{Id = "A117A8B5-F055-4A06-98A6-faxA4CEDBB24",Name = "Member", NormalizedName = "MEMBER",AccessDescription = "Can Subscribe/Unsubscribe on Events, has access to own account manipulations",ConcurrencyStamp = "member-concurrency-stamp",
},
new ApplicationUserRole
{Id = "190F2xxC-7177-4C77-BAd2-9121A40206BB",Name = "Admin",NormalizedName = "ADMIN",AccessDescription = "Can Manipulate with Events, has access to own account manipulations",ConcurrencyStamp = "admin-concurrency-stamp",
},
new ApplicationUserRole{Id = "e2H52d72-326e-4AV3-8f1b-7d1a2c2ed14b",Name = "Owner", NormalizedName = "OWNER",AccessDescription = "Can Manage Roles, can assign Roles to Users",ConcurrencyStamp = "owner-concurrency-stamp",
}
);
}
}

internal sealed class ApplicationUserSeed : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
builder.HasData(
new ApplicationUser
{Id = "BC59E711-CEFD-4088-BB9F-19B19F92170D",Email = "[email protected]",EmailConfirmed = true,LockoutEnabled = true,});
}
}
internal sealed class ApplicationUserRoles : IEntityTypeConfiguration<IdentityUserRole<?????>>
{
public void Configure(EntityTypeBuilder<ApplicationUserRole> builder)
{
builder.HasData(
new ApplicationUserRole{Id = "e2H52d72-326e-4AV3-8f1b-7d1a2c2ed14b",});
}
}

}
namespace EventManager.SqlRepository.EntityConfigurations
{
internal sealed class ApplicationUserRolesConfiguration : IEntityTypeConfiguration<ApplicationUserRole>
{
public void Configure(EntityTypeBuilder<ApplicationUserRole> builder)
{
builder.HasData(
new ApplicationUserRole
{Id = "A117A8B5-F055-4A06-98A6-faxA4CEDBB24",Name = "Member", NormalizedName = "MEMBER",AccessDescription = "Can Subscribe/Unsubscribe on Events, has access to own account manipulations",ConcurrencyStamp = "member-concurrency-stamp",
},
new ApplicationUserRole
{Id = "190F2xxC-7177-4C77-BAd2-9121A40206BB",Name = "Admin",NormalizedName = "ADMIN",AccessDescription = "Can Manipulate with Events, has access to own account manipulations",ConcurrencyStamp = "admin-concurrency-stamp",
},
new ApplicationUserRole{Id = "e2H52d72-326e-4AV3-8f1b-7d1a2c2ed14b",Name = "Owner", NormalizedName = "OWNER",AccessDescription = "Can Manage Roles, can assign Roles to Users",ConcurrencyStamp = "owner-concurrency-stamp",
}
);
}
}

internal sealed class ApplicationUserSeed : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
builder.HasData(
new ApplicationUser
{Id = "BC59E711-CEFD-4088-BB9F-19B19F92170D",Email = "[email protected]",EmailConfirmed = true,LockoutEnabled = true,});
}
}
internal sealed class ApplicationUserRoles : IEntityTypeConfiguration<IdentityUserRole<?????>>
{
public void Configure(EntityTypeBuilder<ApplicationUserRole> builder)
{
builder.HasData(
new ApplicationUserRole{Id = "e2H52d72-326e-4AV3-8f1b-7d1a2c2ed14b",});
}
}

}
123 replies
CC#
Created by ВВассралман on 2/20/2025 in #help
Mapping to avoid data dublicating.(Just help with ideas)
Hello, guys! I have a question. If I have a domain model Customer that is used in services, but at the same time I'm using ASP.NET Core Identity, where the user already has all the properties of my Customer, I end up duplicating data and storing it in the database. My Customer model has no unique properties. Can I somehow configure the logic to map or retrieve information from the AspNetUsers table and map it to Customer (I need the ID or email)?
11 replies
CC#
Created by ВВассралман on 2/16/2025 in #help
I am implementing role-based authentication using ASP.NET Identity. I have issue with assign roles
Hello guys, I am implementing role-based authentication using ASP.NET Identity. I have already added three roles: Member, Admin (can create resources), and Owner (can assign roles, grant or revoke the Admin role). I want to keep my controllers clean and move the logic to the infrastructure layer, but something feels wrong. I can't write proper code to prevent the Owner from demoting themselves. Any ideas?
cs
public sealed record AssignAdminRoleRequest
{
public required string Email { get; set; }
}

public async Task<TokensResponse> AssignAdminRole(AssignAdminRoleRequest request)
{
var user = await _userManager.FindByEmailAsync(request.Email);
if (user is null)
{
throw new AuthenticationException();
}
if (!user.EmailConfirmed)
throw new AuthenticationException($"User :{request.Email} needs to confirm email");

if (await _userManager.IsInRoleAsync(user, "Admin"))
{
throw new IdentityException("User already has this role");
}


var result = await _userManager.AddToRoleAsync(user, "Admin");
if (!result.Succeeded)
throw new IdentityException("Failed to add Role. Try again later");

return await CreateTokenResponce(user);
}
cs
public sealed record AssignAdminRoleRequest
{
public required string Email { get; set; }
}

public async Task<TokensResponse> AssignAdminRole(AssignAdminRoleRequest request)
{
var user = await _userManager.FindByEmailAsync(request.Email);
if (user is null)
{
throw new AuthenticationException();
}
if (!user.EmailConfirmed)
throw new AuthenticationException($"User :{request.Email} needs to confirm email");

if (await _userManager.IsInRoleAsync(user, "Admin"))
{
throw new IdentityException("User already has this role");
}


var result = await _userManager.AddToRoleAsync(user, "Admin");
if (!result.Succeeded)
throw new IdentityException("Failed to add Role. Try again later");

return await CreateTokenResponce(user);
}
4 replies