当我尝试加载我的身份登录页面时,出现以下错误 -
InvalidOperationException:尝试激活“FNSD.Areas.Identity.Pages.Account.LoginModel”时无法解析类型“Microsoft.AspNetCore.Identity.SignInManager`1[FNSD.Areas.Identity.Data.FNSDUser]”的服务。
我的 Program.cs 是 -
namespace FNSD.Web
{
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Release"}.json", optional: true)
.Build();
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
我的 Startup.cs 是 -
using FNSD.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace FNSD.Web
{
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ApplicationDbContextConnection")));
services.AddDefaultIdentity<IdentityUser>(
options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddHttpContextAccessor();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(120);
});
services.AddControllersWithViews();
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 8;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromDays(36500);
options.Lockout.MaxFailedAccessAttempts = 3;
options.Lockout.AllowedForNewUsers = true;
});
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = ".AspNetCore.Identity.Application";
options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
options.SlidingExpiration = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Create accounts for the development environment
//using (var scope = app.ApplicationServices.CreateScope())
//{
// AccountsInitialiser.Initialize(scope.ServiceProvider);
//}
}
else
{
app.UseExceptionHandler("/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.UseStatusCodePagesWithRedirects("/PageNotFound");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/Identity/Account/ExternalLogins", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Manage")));
endpoints.MapGet("/Identity/Account/TwoFactorAuthentication", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Manage")));
endpoints.MapGet("/Identity/Account/PersonalData", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Manage")));
endpoints.MapGet("/Identity/Account/ResendEmailConfirmation", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Manage")));
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
AppDomain.CurrentDomain.SetData("WebRootPath", env.WebRootPath);
}
}
}
ApplicationDBContext 是
using FNSD.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace FNSD.Data
{
public class ApplicationDbContext : IdentityDbContext<FNSDUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
FNSDUSe 是 -
using Microsoft.AspNetCore.Identity;
namespace FNSD.Areas.Identity.Data;
// Add profile data for application users by adding properties to the FNSDUser class
public class FNSDUser : IdentityUser
{
public FNSDUser()
{
}
public FNSDUser(string userName) : base(userName)
{
}
}
最后我的 FNSDContext 是 -
using FNSD.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace FNSD.Data;
public class FNSDContext : IdentityDbContext<FNSDUser>
{
public FNSDContext(DbContextOptions<FNSDContext> 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);
}
}
第一次尝试身份!
任何帮助将不胜感激。
谢谢
嗯,异常消息非常清楚,您的服务集合中没有
SignInManager
描述符。
要解决此问题,您需要像这样添加它:
services
.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager(); // Here