C# 切换到“创建”视图时出现标识问题

C# 切换到“创建”视图时出现标识问题,c#,asp.net-core,C#,Asp.net Core,所以我遵循这个教程:教如何为身份用户制作CRUD 我已经到了一个地步,我得到了两个错误,我认为它们是联系在一起的。首先,我将介绍代码: AdminController.cs public ViewResult Create() => View(); [HttpPost] public async Task<IActionResult> Create(User user) { if (ModelState.IsValid) { AppUser a

所以我遵循这个教程:教如何为身份用户制作CRUD

我已经到了一个地步,我得到了两个错误,我认为它们是联系在一起的。首先,我将介绍代码:

AdminController.cs

public ViewResult Create() => View();

[HttpPost]
public async Task<IActionResult> Create(User user)
{
    if (ModelState.IsValid)
    {
        AppUser appUser = new AppUser
        {
            UserName = user.Name,
            Email = user.Email
        };

        IdentityResult result = await userManager.CreateAsync(appUser, user.Password);
        if (result.Succeeded)
            return RedirectToAction("Index");
        else
        {
            foreach (IdentityError error in result.Errors)
                ModelState.AddModelError("", error.Description);
        }
    }
    return View(user);
}
public class AppUser : IdentityUser
{
}
在运行应用程序后访问“localhost/Admin/Create”链接时,我遇到以下错误:

InvalidOperationException:在尝试激活“Filters.Controllers.AdminController”时,无法解析类型“Microsoft.AspNetCore.Identity.UserManager”“1[Crossion.Models.AppUser]”的服务

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,类型类型,类型requiredBy,bool isDefaultParameterRequired)

然后,我发现Startup.cs中可能有问题,因此,经过一点研究,我添加了这行:
服务.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders()

在尝试修复第一个错误时,我遇到了第二个问题:

HTTP错误500.30-ANCM进程内启动失败

我的Startup.cs类如下所示:

 public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
        services.AddIdentity<AppUser, IdentityRole>().AddEntityFrameworkStores<AppIdentityDbContext>().AddDefaultTokenProviders();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores();
services.AddDbContext(options=>options.UseSqlServer(配置[“ConnectionString:DefaultConnection”]);
services.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
}
那么问题出在哪里呢?我试着密切关注教程,但显然我错过了一些东西。。。或者,自从它被发布后,事情发生了变化。非常感谢您的帮助

HTTP错误500.30-ANCM进程内启动失败

这是由于方法
AddDbContext
AddIdentity
Startup.cs中存在重复项。在注释掉重复的内容后,我把它处理掉了

其次,在\u LoginPartial.cshtml中,我有以下内容:

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@inject-SignInManager-SignInManager
@注入用户管理器用户管理器
我不得不用我的
AppUser
替换
IdentityUser
。这修复了第一个错误

HTTP错误500.30-ANCM进程内启动失败

这是由于方法
AddDbContext
AddIdentity
Startup.cs中存在重复项。在注释掉重复的内容后,我把它处理掉了

其次,在\u LoginPartial.cshtml中,我有以下内容:

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@inject-SignInManager-SignInManager
@注入用户管理器用户管理器
我不得不用我的
AppUser
替换
IdentityUser
。这修复了第一个错误