因此,我正在遵循本教程:https://www.yogihosting.com/aspnet-core-identity-create-read-update-delete-users/教如何为身份用户制作CRUD。
我到了2个错误的地步。首先,我将介绍代码:
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);
}
AppUser.cs
public class AppUser : IdentityUser
{
}
在运行应用程序后访问“ localhost / Admin / Create”链接时,出现此错误:
InvalidOperationException:尝试激活“ Filters.Controllers.AdminController”时,无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager`1 [Intersection.Models.AppUser]”的服务。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,type requiredBy,bool isDefaultParameterRequired)
然后,我发现 Startup.cs 中可能出了一些问题,因此,经过一些研究,我添加了以下行:services.AddIdentity<AppUser, IdentityRole>().AddEntityFrameworkStores<AppIdentityDbContext>().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?}");
});
}
}
那么可能是什么问题?我尝试了紧跟本教程,但显然我漏掉了一些东西。非常感谢您的帮助!
答案 0 :(得分:0)
HTTP错误500.30-ANCM进程内启动失败
这是由于方法AddDbContext
和AddIdentity
在 Startup.cs 中重复。在注释掉重复项之后,我将其删除。
其次,在 _LoginPartial.cshtml 中,我有以下内容:
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
我不得不用IdentityUser
替换AppUser
。这解决了第一个错误。