【问题标题】:Identity not working in .NET Core project [duplicate]身份在 .NET Core 项目中不起作用 [重复]
【发布时间】:2020-05-22 18:48:42
【问题描述】:

我创建了一个没有身份的 .NET Core 项目,然后尝试从脚手架添加身份。我的启动代码是:

public System.IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ProjectDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<Microsoft.AspNetCore.Identity.IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ProjectDbContext>();

    ...
}

但是在调用身份表单时它不起作用,并且标头 URL 是:

http://localhost:5000/?area=Identity&page=%2FAccount%2FLogin

我错过了什么?

【问题讨论】:

  • 如果链接的解决方案无法解决此问题,请联系我,但应该解决。

标签: c# asp.net-core asp.net-identity


【解决方案1】:

我不知道你的问题到底是什么,但如果你想手动添加身份,也许这段代码对你有用,只需将它放在启动类的 ConfigureService 中:

 services.AddIdentity<ApplicationUser, IdentityRole>()
           .AddEntityFrameworkStores<ProjectDbContext>()
           .AddDefaultTokenProviders();
   services.AddDbContext<ProjectDbContext>(options => 
             option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

并确保您的 ProjectDbContext 继承自 IdentityDbContext , 对于你的ApplicationUser,如果你愿意,你可以使用IdentityUser,或者你可以通过从IdentityUser继承来自定义你的ApplicationUser,并且在这两种方式上不要添加任何DbSetProjectDbContext,Asp Core Identity 会为你添加这个。

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 2017-12-19
    • 2017-08-03
    • 1970-01-01
    • 2019-05-19
    • 2017-05-01
    • 2020-06-05
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多