Identity User Nie mogę utworzyć użytkownika

0

Witam.

Tworzę api i mam problem z utworzeniem użytkownika.
Stworzyłem metodę w api która przyjmuje dane (działa, sprawdzane w postmanie). Ale finalnie problem leży w tym, że nie zapisuje mi tego w bazie.

Mój DbContext dziedziczy po IdentityDbContext.


using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace WGCM.DATA
{
    public class AppDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Client> Clients {get;set;}
        

    public AppDbContext(DbContextOptions<AppDbContext> options) :  base(options)
    {
        
    }

    }
}

To jest moja klasa ApplicationUser

using Microsoft.AspNetCore.Identity;

namespace WGCM.DATA
{
    public class ApplicationUser : IdentityUser
    {
        public string BH { get; set; }
    }
}

Wydaje mi się, że problem może polegać w konfiguracji Startup.cs ale nie mogę znaleźć przyczyny. Baza danych działa tworzy wszystkie tabele, mogę tworzyć inne zapytania wykorzystując np sam dbcontext.Add() i następnie SaveChanges()

Baza jaką wykorzystuję to MySQL.

Tutaj mój Startup.cs

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WGCM.BUSINESS.Mappers;
using WGCM.DATA;

namespace WGCM.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentity<ApplicationUser,IdentityRole>()
                .AddEntityFrameworkStores<AppDbContext>()
                .AddDefaultTokenProviders();

             services.Configure<IdentityOptions>(options =>
            {
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireDigit = false;
            });
 
            services.AddDbContext<AppDbContext>(options => options.UseMySQL("SERVER=xxx.xxx.xxx.xxx;database=Test666;UID=user;PASSWORD=pass"));
            services.AddControllers();
            services.AddScoped<SettingMapper>();
            services.AddScoped<IClientRepository,ClientRepository>();
            services.AddScoped<IUserRepository,UserRepository>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            var database = serviceProvider.GetService<AppDbContext>();
            database.Database.EnsureCreated();
        }
    }
}

Oraz moja metoda do rejestracji

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
// using Microsoft.AspNetCore.Mvc;
using WGCM.BUSINESS.Mappers;
using WGCM.DATA;

namespace WGCM.API.Controllers
{
    public class RegisterController : ControllerBase
    {
        private readonly SettingMapper _mapper;
        private readonly IServiceProvider _serviceProvider;
        private readonly UserManager<ApplicationUser> _identityUser;
        public RegisterController(SettingMapper mappe,IServiceProvider serviceProvider,UserManager<ApplicationUser> identityUser)
        {
            _mapper = new SettingMapper();
            _serviceProvider = serviceProvider;
            _identityUser = identityUser;
        }

        [HttpPost]
        [Route("[controller]")]
        public void Post([FromBody] ApplicationUser user)
        {
            var newUser = new ApplicationUser{
                Email = user.Email,
                BH = user.BH
            };
        
            _identityUser.CreateAsync(newUser,user.PasswordHash);
        }

    }
}

Proszę o pomoc :)

0

A gdzie zapisujesz transakcje? Bo po tym kodzie tego nie widać.

2

Jak dla mnie to problem masz w braku async , await w metodzie controllera

1 użytkowników online, w tym zalogowanych: 0, gości: 1