简体   繁体   English

来自 api 的实体框架身份注册,具有自定义身份用户

[英]Entityframework identity register from api with custom identityUser

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.我需要使用我的 Asp.Net API 创建一个 ApplicationUser(从 IdentityUser 派生的 class),我需要在我的 Asp.net MVC 使用的同一数据库中创建它。 My request works since it gives me 200 response but my user is not in the AspNetUser table autogenerated by EntityFramework.我的请求有效,因为它给了我 200 个响应,但我的用户不在 EntityFramework 自动生成的 AspNetUser 表中。

[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这是我的 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这是我的 api 的启动

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

    }

this is my ApplicationUser class in the Core project这是我在核心项目中的 ApplicationUser class

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?我需要为我的 ApplicationUser 创建我的 on Create 方法还是我遗漏了什么?

Check that the DefaultConnection points to the correct database in appsettings.json AND appsettings.Developer.json if that file exists.检查 DefaultConnection 是否指向 appsettings.json 和 appsettings.Developer.json 中的正确数据库(如果该文件存在)。

I'm also assuming userManager.CreateAsync works correctly and is just part of AspNetCore.Identity.我还假设 userManager.CreateAsync 工作正常并且只是 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.在 Visual Studio 中,这可以通过右键单击项目、选择添加、选择项目引用并检查要引用的项目来完成。

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.在这里,您可以管理整个数据访问层 (DAL),并在任何其他项目中引用它,而无需担心在可能发生冲突的两个位置维护它。

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.您需要从现有位置添加用户,假设您由于 AspNetUser 表而首先使用代码。

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. TL;DR 将您的数据访问层(模型、dbContext、迁移等)添加到 C# 共享项目中,并在您的 MVC 和 Web API 项目中引用它以保持未来工作的一致性并减少维护工作的一致性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有从 ApplicationUser 到 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的隐式引用转换 - There is no implicit reference conversion from ApplicationUser to Microsoft.AspNet.Identity.EntityFramework.IdentityUser &#39;Microsoft.AspNet.Identity.EntityFramework.IdentityUser&#39;未标记为可序列化 - 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' is not marked as serializable Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的外键? - Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser? 为什么IdentityUser类在Microsoft.AspNet.Identity.EntityFramework命名空间中而不在Core包中? - Why is the IdentityUser class in the Microsoft.AspNet.Identity.EntityFramework namespace and not in the Core package? ASP.NET身份IdentityUser自定义属性导致运行时错误 - Asp.NET Identity IdentityUser Custom Properties Cause Runtime Error 从EntityFramework将身份值映射到DTO - Mapping Identity value into DTO from Entityframework 自定义Microsoft.AspNet.Identity.EntityFramework.UserStore <TUser> - custom Microsoft.AspNet.Identity.EntityFramework.UserStore<TUser> 自定义属性为null IdentityUser - Custom property is null IdentityUser .NET 6 身份自定义 - 错误:没有从 ApplicationUser 到 IdentityUser 的隐式引用转换<string></string> - .NET 6 Identity Customization - Error: There is no implicit reference conversion from ApplicationUser to IdentityUser<string> 无法从“字符串”转换为“ Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole - cannot convert from 'string' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM