我正在尝试使用Identity Core 2.0和我自己的自定义表和EF Core 2.0构建身份验证功能 .

我使用它作为参考:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-2.1 .

问题是我没有使用角色,也没有使用声明,所以我有一个表/ DbSet 包含用户信息 . 我已经实现了所需的接口,一切看起来都很好 . 我也在使用JWT身份验证 . 问题是我不知道如何在Startup中配置服务 . 这是我目前的代码:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,

                ValidIssuer = Configuration["JwtIssuer"],
                ValidAudience = Configuration["JwtAudience"],
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"]))
            };
        });

    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));           

    services.AddIdentity<User, IdentityRole>()
        //.AddEntityFrameworkStores<MyDbContext>()
        .AddDefaultTokenProviders();

    services.AddTransient<IUserRepository, UserRepository>();
    services.AddTransient<IUserStore<User>, UserStore>();
    services.AddTransient<IEmailSender, EmailSender>();

    services.AddMvc();
}

其中 User 是我自定义的用户类实现 . 同样适用于 UserStore .

但是,我不确定如何配置特定的身份服务 . 我应该调用 services.AddIdentity ,但是它需要一个类型为 TRole 的参数,我没有实现,没有提供角色类型也没有任何变化 . 我要么必须实现虚拟/使用 IdentityRole ,要么我必须提取扩展方法的内容并手动添加它们,这对我来说没有切割它 . 这是我发现的扩展方法的主体:ASP.NET Identity Ref-1 / ASP.NET Identity Ref-2 .

我该如何注册服务?另外,考虑到情况(JWT,自定义表的身份),我是否在配置中遗漏了其他内容 .