Home > Net >  Entityframework identity register from api with custom identityUser
Entityframework identity register from api with custom identityUser

Time:12-19

I need to create an ApplicationUser (a derived class from IdentityUser) with my Asp.Net API, I need to create it in the same database that my Asp.net MVC uses. My request works since it gives me 200 response but my user is not in the AspNetUser table autogenerated by EntityFramework.

[HttpPost]
    public async Task<ActionResult> Register(RegisterDTO register)
    {
        ApplicationUser user = new ApplicationUser();
        if (register.Password == register.PasswordConfirm)
        {
            user.UserName = register.Email;
            user.Email = register.Email;
        }
        else
        {
            return StatusCode(StatusCodes.Status500InternalServerError, new { Message = "Le mot de passe et sa confirmation ne sont pas identiques." });
        }
        var identityResult = await userManager.CreateAsync(user, register.Password);
        if (!identityResult.Succeeded)
        {
            return StatusCode(StatusCodes.Status500InternalServerError, new { Error = identityResult.Errors });
        }
        return Ok();
    }

this is my register from my api

services.AddDbContext<CegesDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        services.AddScoped<ICegesServices, CegesServices>();
        services.AddIdentity<ApplicationUser, IdentityRole>()
                       .AddDefaultTokenProviders().AddDefaultUI()
                       .AddEntityFrameworkStores<CegesDbContext>();

this is the startup from my api

    public class ApplicationUser : IdentityUser
    {
        public List<Entreprise> Entreprises { get; set; }

    }

this is my ApplicationUser class in the Core project

I'm not sure what to do here. Do I need to create my on Create method for my ApplicationUser or am I missing something?

CodePudding user response:

Check that the DefaultConnection points to the correct database in appsettings.json AND appsettings.Developer.json if that file exists.

I'm also assuming userManager.CreateAsync works correctly and is just part of AspNetCore.Identity.

My next advice would be to not split your models / data access layer across two projects like this. Instead create a Shared Project and reference your other projects from that.

In Visual Studio, this can be done by right clicking the project, selecting add, choose Project Reference and check the projects to reference.

In here you can manage your entire data access layer (DAL), and reference it in any other projects without need to worry about maintaining it in two locations that might be conflicting.

You can't update the database with your users migration if the database migration records do not match up. You'll need to add the users from the existing location, assuming you're using code first due to the AspNetUser table.

TL;DR Add your data access layer (models, dbContext, migrations etc.) to a C# Shared Project and reference this in your MVC and Web API projects to keep consistency and reduce future work on maintenance.

  • Related