0

I'm very new with those things, so I have some problems with understanding and figuring out which approach to use.

Currently, I am using ASP.NET Core 6 and IdentityServer4 in my project. I am configuring my authorization server and there I have some controllers for creating users using API to authorize.

In the controller for users handling I am using UserManager for all of the CRUD operations. Is that better approach than using dbContext? I add user by writing API in Server that uses Authorize to check token.

But I configure UserManager<IdentityUser> with

builder.Services.AddIdentity<IdentityUser, IdentityRole>()   
                .AddEntityFrameworkStores<AspNetIdentityDbContext>()
                .AddDefaultTokenProviders();

But with this configuration, Authorize does not accept. And I don't know what to do to fix this error.

Do you have some better solution?

Program.cs:

builder.Services.AddTransient<IAccUserRepon, AccUserRepon>(); 
builder.Services.AddIdentity<IdentityUser, IdentityRole>()     
                .AddEntityFrameworkStores<AspNetIdentityDbContext>()
                .AddDefaultTokenProviders();

AccUserController:

[HttpPost]
[Authorize]
public async Task<IActionResult> CreateAccUser([FromBody] CreateAccUser createAccUser) 
{
    Console.WriteLine("vui");

    var result = await _accUserRepon.AccUserCreate(createAccUser);

    if (result.Succeeded) 
    {
        return Ok(1);
    }
    else 
    { 
        return Ok(result); 
    }

    return Ok("vyi");
}

AccUserRepon:

public class AccUserRepon : IAccUserRepon
{
    private readonly AspNetIdentityDbContext _aspNetIdentityDbContext;
    private readonly UserManager<IdentityUser> _userManager;

    public AccUserRepon(AspNetIdentityDbContext context, UserManager<IdentityUser> userManager)
    {
        _aspNetIdentityDbContext = context;
        _userManager = userManager;
    }

    public async Task<IdentityResult> AccUserCreate(CreateAccUser createAccUser)
    {
        // using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
        IdentityUser user = new IdentityUser
                                {
                                    Email = createAccUser.Email,
                                    UserName = createAccUser.UserName
                                };
        IdentityResult result = null;

        try 
        {
            result = await _userManager.CreateAsync(user, createAccUser.Password);

            if (result.Succeeded)
            {
                result = _userManager.AddClaimsAsync(user, new Claim[]
                                      {
                                          new Claim(JwtClaimTypes.Name, createAccUser.UserName + " " + createAccUser.LastName),
                                          new Claim(JwtClaimTypes.GivenName, createAccUser.UserName),
                                          new Claim(JwtClaimTypes.FamilyName, createAccUser.LastName),
                                          new Claim(JwtClaimTypes.WebSite, "http://"+createAccUser.UserName + createAccUser.LastName+".com"),
                                          new Claim("location", "somewhere")
                                       }).Result;
            }
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex.Message);
        }

        return result;            
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Long
  • 1
  • see my answer here, might gives some ideas, https://stackoverflow.com/questions/76346382/authentication-and-authorization-using-identityserver4-along-with-microsoft-aspn/76346696#76346696 – Tore Nestenius May 27 '23 at 11:32
  • Your intention is unclear. If remove [Authorize] it will work. What is the [Authorize] for, the identityServer4 jwt token? Which flow are you using in identiyserver clientcrediential/code/implict ? So do you mean you request a token from ids using existing user first? Then use this token to create a new user? – Qiang Fu May 29 '23 at 07:26

0 Answers0