Hi forum members,
As the question suggests, I am facing some difficulty in updating user roles with other data through my code. I am unable to visualize how to do it. Let me explain the scenario in detail.
I have used .netcore Identity and EntityFramework to set up the database for my application. I modified my IdentityUser entity so that it contains some additional fields like FullName, MobileNumber and SubscriptionType. SubscriptionType can be Monthly, Quarterly or Yearly. The AspNetRoles table has 4 roles in it like Actor, Musician, Painter, Photographer. While registration/creating new user I am providing the requisite info which includes Role also and all the data is being saved to the AspNetUsers and AspNetUserRoles tables as expected. I am able to fetch the data for a logged in user. What I want next is to provide Update functionality so that the logged in user can update his/her MobileNumber, SubscriptionType and Role. This is where my knowledge is falling short as I am unable to construct the code. Needless to mention, only an authorized user should be able to update self profile.
Surely the update operation should be based on user id of the logged in entity. I understand that the RoleId in AspNetUserRoles table should get updated with the new value. Also, the code block should check if the edited new MobileNumber exists in the database and should give error. This is my requirement. I am using minimal API approach in my project . Let me share some code from my project so you can see the parts that I have put in place till now.
Program.cs -
using ForSideSystems.API.Data;
using ForSideSystems.API.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;
using ForSideSystems.API.Extensions;
using ForSideSystems.API.Controllers;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
… blablabla
- blablabla
- blablabla
app.UseDefaultFiles();
app.UseStaticFiles();
app.MapControllers();
app.MapGroup("/api/Accounts")
.MapAccountEndpoints();
app.Run();
AccountEndpoints.cs –
public static class AccountEndpoints
{
public static IEndpointRouteBuilder MapAccountEndpoints(this IEndpointRouteBuilder app)
{
app.MapGet("/userprofile", GetUserProfile);
//app.MapPut("/updateuserprofile", UpdateUserProfile);
//or I am not sure could be
// app.MapPost("/updateuserprofile", UpdateUserProfile);
return app;
}
[Authorize]
private static async Task<IResult> GetUserProfile(ClaimsPrincipal user, UserManager<AppUser> userManager)
{
string userName = user.Claims.First(x => x.Type == "name").Value;
string role = user.FindFirst(ClaimTypes.Role).Value;
string currentUserID = user.FindFirst(ClaimTypes.NameIdentifier).Value;
var loggedInUser = await userManager.FindByIdAsync(currentUserID);
return Results.Ok(new
{
FullName = userName,
Email = loggedInUser.Email,
Mobile = loggedInUser.MobileNumber,
SubscriptionType = loggedInUser.SubscriptionType,
Role=role
});
}
}
AppUser.cs -
public class AppUser : IdentityUser
{
public string FullName { get; set; } = string.Empty;
public string MobileNumber { get; set; } = string.Empty;
public string SubscriptionType { get; set; } = string.Empty;
}
As you can see, inside the AccountEndpoints.cs class there is GetUserProfile method to fetch and show logged in user details. I am parsing the jWT token sent from swagger authorize facility to get my necessary information. If you need more code block from my existing and working code base, please do ask, I will be more than glad to share.
Please help me construct the logic and the code for the UpdateUserProfile method. Some help needed from you on this scenario. Been stuck with this for few days now.
Thanks in advance,