ASP.Net Core 3.1 Identity中主键/ID使用Guid而不是字符串

huangapple go评论71阅读模式
英文:

Guid Instead of string for primary key / ID for ASP.Net Core 3.1 Identity

问题

这不再适用于ASP.Net Core 3.1 / .Net Core 3.1。

编译错误:'IdentityBuilder' 不包含 'AddEntityFrameworkStores' 的定义,也没有可访问的扩展方法 'AddEntityFrameworkStores',接受类型为 'IdentityBuilder' 的第一个参数(您是否缺少 using 指令或程序集引用?) WebSite.Site C:\WorkSource....\Startup.cs 32 Active

public class ApplicationUser : IdentityUser<Guid> { }
public class Role : IdentityRole<Guid> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
   ...
}

public class Startup
{
    ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<ApplicationUser, Role>()
            .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
            .AddDefaultTokenProviders()
            .AddUserStore<UserStore<ApplicationUser, Role, ApplicationDbContext, Guid>>()
            .AddRoleStore<RoleStore<Role, ApplicationDbContext, Guid>>();

    }
}

如果只是删除 Guid 泛型参数,像这样:

.AddEntityFrameworkStores<ApplicationDbContext, Guid>()

那么会出现浏览器错误:此本地主机页面无法找到找不到网页,网址为:http://localhost:50827/Account/Login?ReturnUrl=%2F

我应该如何做这个?谢谢。

英文:

This no longer works with ASP.Net Core 3.1 / .Net Core 3.1

https://stackoverflow.com/a/37173202/1698480

Compile error:'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores' and no accessible extension method 'AddEntityFrameworkStores' accepting a first argument of type 'IdentityBuilder' could be found (are you missing a using directive or an assembly reference?) WebSite.Site C:\WorkSource....\Startup.cs 32 Active

public class ApplicationUser : IdentityUser&lt;Guid&gt; { }
public class Role : IdentityRole&lt;Guid&gt; { }

public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, Role, Guid&gt;
{
   ...
}

public class Startup
{
    ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity&lt;ApplicationUser, Role&gt;()
            .AddEntityFrameworkStores&lt;ApplicationDbContext, Guid&gt;()
            .AddDefaultTokenProviders()
            .AddUserStore&lt;UserStore&lt;ApplicationUser, Role, ApplicationDbContext, Guid&gt;&gt;()
            .AddRoleStore&lt;RoleStore&lt;Role, ApplicationDbContext, Guid&gt;&gt;();

    }
}

If I just remove the Guid generic arg like this:

.AddEntityFrameworkStores&lt;ApplicationDbContext, Guid&gt;()

then I get browser error: This localhost page can’t be foundNo webpage was found for the web address: http://localhost:50827/Account/Login?ReturnUrl=%2F

How can I do this? thanks

答案1

得分: 2

这部分内容的中文翻译如下:

这个第一部分与以前一样:

public class ApplicationUser : IdentityUser<Guid> { }
public class Role : IdentityRole<Guid> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
   ...
}

然后,你可以像这样完成剩下的部分:

public class Startup
{
     
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<ApplicationUser, Role>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders()
            .AddUserStore<UserStore<ApplicationUser>>()
            .AddRoleStore<RoleStore<Role>>();
    }
}
   
请注意,ASP.NET Core 3.1 已经变得非常聪明,可以自动识别各种细节,例如您在身份验证中使用了 GUID。您甚至不需要在您的 Startup 类中指定 ApplicationDbContext 类型、ApplicationUser 类型和 Role 类型;当您提及这些类时,它会通过反射查找并自动解析详细信息。
英文:

This first part is just like before:

public class ApplicationUser : IdentityUser&lt;Guid&gt; { }
public class Role : IdentityRole&lt;Guid&gt; { }

public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, Role, Guid&gt;
{
   ...
}

And then you do the rest like this:

public class Startup
{
    … 
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity&lt;ApplicationUser, Role&gt;()
            .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
            .AddDefaultTokenProviders()
            .AddUserStore&lt;UserStore&lt;ApplicationUser&gt;&gt;()
            .AddRoleStore&lt;RoleStore&lt;Role&gt;&gt;();

    }
}

Note that ASP.NET Core 3.1 has become quite clever in figuring everything out, such as identifying that you are using GUIDs for identity. You don’t even need to specify the ApplicationDbContext type, ApplicationUser type, and Role type in your Startup; when you mention these classes, it just looks them up via reflection and figures out the details by itself.

huangapple
  • 本文由 发表于 2020年1月4日 12:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定