C# 使用ASP.NET核心标识时,未配置任何身份验证处理程序来处理方案:Microsoft.AspNet.Identity.Application
尝试登录用户时会显示错误:C# 使用ASP.NET核心标识时,未配置任何身份验证处理程序来处理方案:Microsoft.AspNet.Identity.Application,c#,asp.net-identity,asp.net-core,asp.net-identity-3,C#,Asp.net Identity,Asp.net Core,Asp.net Identity 3,尝试登录用户时会显示错误: public async Task<bool> SignIn(string email, string password) { var user = await this._userManager.FindByEmailAsync(email); if (user == null) return false; if (await this._userManager.CheckPasswordAsync(user,
public async Task<bool> SignIn(string email, string password)
{
var user = await this._userManager.FindByEmailAsync(email);
if (user == null)
return false;
if (await this._userManager.CheckPasswordAsync(user, password))
{
await this._signInManager.SignInAsync(user, false); //error
return true;
}
return false;
}
公共异步任务登录(字符串电子邮件、字符串密码)
{
var user=等待此消息。\u userManager.findbyemailsync(电子邮件);
if(user==null)
返回false;
if(等待此消息。\u userManager.CheckPasswordAsync(用户,密码))
{
等待此消息。_signInManager.SignInAsync(user,false);//错误
返回true;
}
返回false;
}
My Startup.cs如下所示:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RoleManager<IdentityRole>>();
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<SignInManager<ApplicationUser>>();
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<DbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddMvc(config =>
{
#if !DEBUG
config.Filters.Add(new RequireHttpsAttribute());
#endif
}).AddJsonOptions(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}); ;
services.AddSingleton<IAccountService, AccountService>();
services.AddSingleton<IAgencyService, AgencyService>();
services.AddSingleton<IFeatureService, FeatureService>();
....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<DbContext>()
.Database.Migrate();
}
}
catch { }
}
app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
... routes
});
SeedGenerator.Initialize(app.ApplicationServices);
}
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true);
if(env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;set;}
public void配置服务(IServiceCollection服务)
{
services.AddTransient();
services.AddTransient();
services.AddTransient();
//添加框架服务。
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>
使用SQLServer(配置[“数据:默认连接:连接字符串]);
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc(配置=>
{
#如果!调试
添加(新的requireHttpAttribute());
#恩迪夫
}).AddJsonOptions(选项=>
{
opts.SerializerSettings.ContractResolver=新的CamelCasePropertyNamesContractResolver();
}); ;
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
....
}
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
尝试
{
使用(var serviceScope=app.ApplicationServices.GetRequiredService()
.CreateScope())
{
serviceScope.ServiceProvider.GetService()
.Database.Migrate();
}
}
捕获{}
}
app.UseIISPlatformHandler(options=>options.AuthenticationDescriptions.Clear());
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(路由=>
{
…路线
});
初始化(app.ApplicationServices);
}
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
我认为您不应该将标识组件的作用域定义为singleton,它们应该根据每个请求确定作用域,不确定这是否是问题所在,但我认为这是不对的。由于您没有使用services.AddIdentity调用services.AddAuthentication,因此您也应该这样做并在代码中显示它,但我在services.AddIdentity之后立即调用了services.AddIdentity。addentityFramework我将尝试更改singleton部分您对add rolemanager等的其他调用应该在调用AddIdentity之前,因为它是同时注册那些你需要的组件,让你的第一个仍然是错误:(虽然这是好的观点。)