C# 是否可以在空的ASP.NET核心Mvc项目中打开身份验证?

C# 是否可以在空的ASP.NET核心Mvc项目中打开身份验证?,c#,authentication,asp.net-core,asp.net-core-mvc,C#,Authentication,Asp.net Core,Asp.net Core Mvc,在我的项目中,我陷入了身份验证的困境。我创建了没有身份验证的asp.net mvc项目。我有现有的数据库。我通过执行:Scaffold Dbcontext添加了db,并在Startup.cs(ConfigureServices方法)中使用它: 我有下一个控制器: private readonly UserManager<IdentityUser> _userManager; private readonly SignInManager<IdentityUser

在我的项目中,我陷入了身份验证的困境。我创建了没有身份验证的asp.net mvc项目。我有现有的数据库。我通过执行:
Scaffold Dbcontext
添加了db,并在Startup.cs(ConfigureServices方法)中使用它:

我有下一个控制器:

 private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly AppContext _context;
        public HomeController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, AppContext context)
        {
            this._userManager = userManager;
            this._signInManager = signInManager;
            _context = context;
        }
有人能解释一下如何在空项目中使用身份验证吗?或者我需要用个人身份重新创建项目?(第二个瓦里安会伤害我)

编辑#1 以下是其他信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace App.Chat.Identity
{
    public class ApplicationUser : IdentityUser
    {
    }
}
和启动信息:

services.AddEntityFrameworkSqlServer().AddDbContext<AppContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<AppContext>()
                .AddDefaultTokenProviders();
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“SqlDb”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
编辑#2 我的AppContext:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using App.Chat.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;


namespace App.Chat.TModels
{
    public partial class AppContext : IdentityDbContext<ApplicationUser>
    {
public AppContext(DbContextOptions<AppContext> options)
    : base(options)
        { }
}
使用系统;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore.Metadata;
使用App.Chat.Identity;
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
名称空间App.Chat.TModels
{
公共部分类AppContext:IdentityDbContext
{
公共AppContext(DbContextOptions选项)
:基本(选项)
{ }
}

您的错误是Microsoft.AspNet.Identity.EntityFramework.IdentityUser,它不是ASP.NET核心包。它是用于旧框架的


检查您的nuget软件包并卸载
Microsoft.AspNet.Identity.EntityFramework
Microsoft.AspNet.Identity.Core

,就像Kahbazi建议的更改名称一样。然后检查这两个类

 //change in startup.
//services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(options =>


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

public class ApplicationUser : IdentityUser
{
}

 public class HomeController
{
    private readonly UserManager<IdentityUser> _userManager;
    private readonly SignInManager<IdentityUser> _signInManager;
    private readonly ApplicationDbContext _context;
    public HomeController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, ApplicationDbContext context)
    {
        this._userManager = userManager;
        this._signInManager = signInManager;
        _context = context;
    }
}
//启动中的更改。
//services.AddEntityFrameworkSqlServer().AddDbContext(选项=>

公共类ApplicationDbContext:IdentityDbContext { 公共应用程序DBContext(DbContextOptions选项) :基本(选项) { } 模型创建时受保护的覆盖无效(ModelBuilder) { 基于模型创建(生成器); //自定义ASP.NET标识模型,并根据需要覆盖默认值。 //例如,可以重命名ASP.NET标识表名称等。 //在调用base.OnModelCreating(builder)后添加自定义项; } } 公共类应用程序用户:IdentityUser { } 公共类家庭控制器 { 私有只读用户管理器_UserManager; 专用只读签名管理器\u签名管理器; 私有只读应用程序的bContext\u上下文; 公共家庭控制器(UserManager UserManager、SignInManager、SignInManager、ApplicationDbContext上下文) { 这是.\u userManager=userManager; 这是.\u signInManager=signInManager; _上下文=上下文; } }
“类型”App.Chat.TModels.AppContext不能用作泛型类型或方法“IdentityEntityFrameworkBuilderExtensions.AddEntityFrameworkStores(IdentityBuilder)中的类型参数“TContext”“。没有从“App.Chat.TModels.AppContext”到“Microsoft.EntityFrameworkCore.DbContext”的隐式引用转换。您应该已经接近了。在“配置”下启动时,您可能还需要App.UseAuthentication();为什么不创建一个新的project webapplication并更改身份验证以在应用程序中存储用户帐户。然后比较Startup add take您缺少的内容。@ivw,我使用了“app.UseAuthentication();”如何实现DbContext?像这样?公共类ApplicationDbContext:IdentityDbContext{public ApplicationDbContext(DbContextOptions选项):基(选项){}是的,它可以正常工作。但我遇到了新的异常:
System.invalidoOperationException:“实体类型'IdentityUserLogin'需要定义主键。”
是的,我们忘记了更改homecontroller以使用ApplicationDbContext,我添加了它。删除数据库并让它重新创建。AppContext如果以前创建了数据库,则不会有它,但现在将您的新代码在ApplicationDbContextModelSnapshot.cs 193)中提到。删除数据库,您的意思是删除所有模型并再次使用scaffold dbcontext吗?我无法理解,这取决于您的连接字符串。是localbd查找文件并删除。例如“DefaultConnection”:“Server=(localdb)\\mssqllocaldb;数据库=aspnet-WebApplication2-53bc9b9d-9d6a-45d4-8429-2a2761773502;可信连接=True;多个活动结果集=True”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace App.Chat.Identity
{
    public class ApplicationUser : IdentityUser
    {
    }
}
services.AddEntityFrameworkSqlServer().AddDbContext<AppContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<AppContext>()
                .AddDefaultTokenProviders();
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using App.Chat.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;


namespace App.Chat.TModels
{
    public partial class AppContext : IdentityDbContext<ApplicationUser>
    {
public AppContext(DbContextOptions<AppContext> options)
    : base(options)
        { }
}
 //change in startup.
//services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(options =>


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

public class ApplicationUser : IdentityUser
{
}

 public class HomeController
{
    private readonly UserManager<IdentityUser> _userManager;
    private readonly SignInManager<IdentityUser> _signInManager;
    private readonly ApplicationDbContext _context;
    public HomeController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, ApplicationDbContext context)
    {
        this._userManager = userManager;
        this._signInManager = signInManager;
        _context = context;
    }
}