C# InvalidOperationException:没有为方案注册身份验证处理程序';CookieSettings';

C# InvalidOperationException:没有为方案注册身份验证处理程序';CookieSettings';,c#,asp.net-mvc,core,C#,Asp.net Mvc,Core,我正在使用ASP.NETMVCCore2.1开发一个应用程序,在这个应用程序中,我不断遇到以下异常 InvalidOperationException:没有为方案“CookieSettings”注册身份验证处理程序。注册的方案为:Identity.Application、Identity.External、Identity.TwoFactorMemberMe、Identity.TwoFactorUserId。是否忘记调用AddAuthentication().AddSomeAuthHandler

我正在使用ASP.NETMVCCore2.1开发一个应用程序,在这个应用程序中,我不断遇到以下异常

InvalidOperationException:没有为方案“CookieSettings”注册身份验证处理程序。注册的方案为:Identity.Application、Identity.External、Identity.TwoFactorMemberMe、Identity.TwoFactorUserId。是否忘记调用AddAuthentication().AddSomeAuthHandler

我浏览了一些文章,做了必要的修改,但例外情况仍然是一样的。我不知道下一步该怎么办

以下是我的startup.cs:

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<Infrastructure.Data.ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<Infrastructure.Data.ApplicationDbContext>();

        services.AddSingleton(Configuration);

        //Add application services.
       services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        //Add services
        services.AddTransient<IContactManagement, ContactManagement>();
        services.AddTransient<IRepository<Contact>, Repository<Contact>>();
        services.AddTransient<IJobManagement, JobJobManagement>();
        services.AddTransient<IRepository<Job>, Repository<Job>>();
        services.AddTransient<IUnitManagement, UnitManagement>();
        services.AddTransient<IRepository<Sensor>, Repository<Sensor>>();
        services.AddTransient<IJobContactManagement, JobContactManagement>();
        services.AddTransient<IRepository<JobContact>, Repository<JobContact>>();
        services.AddTransient<IJobContactEventManagement, JobContactEventManagement>();
        services.AddTransient<IRepository<JobContactEvent>, Repository<JobContactEvent>>();
        services.AddTransient<IJobsensorManagement, JobsensorManagement>();
        services.AddTransient<IRepository<JobSensor>, Repository<JobSensor>>();
        services.AddTransient<IEventTypesManagement, EventTypesManagement>();
        services.AddTransient<IRepository<EventType>, Repository<EventType>>();
        services.AddTransient<IJobSensorLocationPlannedManagement, JobSensorLocationPlannedManagement>();
        services.AddTransient<IRepository<JobSensorLocationPlanned>, Repository<JobSensorLocationPlanned>>();
        services.AddTransient<IDataTypeManagement, DataTypeManagement>();
        services.AddTransient<IRepository<DataType>, Repository<DataType>>();
        services.AddTransient<IThresholdManegement, ThresholdManegement>();
        services.AddTransient<IRepository<Threshold>, Repository<Threshold>>();
        services.AddTransient<ISeverityTypeManagement, SeverityTypeManagement>();
        services.AddTransient<IRepository<SeverityType>, Repository<SeverityType>>();
        services.AddTransient<ISensorDataManagement, SensorDataManagement>();
        services.AddTransient<IRepository<SensorData>, Repository<SensorData>>();
        services.AddTransient<ISensorLocationManagement, SensorLocationManagement>();
        services.AddTransient<IRepository<SensorLocation>, Repository<SensorLocation>>();

        services.AddTransient<ISensorStatusTypeManagement, SensorStatusTypeManagement>();
        services.AddTransient<IRepository<SensorStatusType>, Repository<SensorStatusType>>();
        services.AddTransient<IRepository<SensorDataToday>, Repository<SensorDataToday>>();
        services.AddTransient<ISensorDataTodayManagement, SensorDataTodayManagement>();

        services.AddSession();


        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(20);
        });

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
               .AddCookie(options =>
               {
                   options.LoginPath = "/Account/Login/";
                   options.LogoutPath = "/Account/Logout/";
               });

        services.AddAuthentication(options =>
        {
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // 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");
            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()
.AddEntityFrameworkStores();
services.AddSingleton(配置);
//添加应用程序服务。
services.AddTransient();
services.AddTransient();
//添加服务
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddSession();
services.AddSession(选项=>
{
options.IdleTimeout=TimeSpan.FromMinutes(20);
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(选项=>
{
options.LoginPath=“/Account/Login/”;
options.LogoutPath=“/Account/Logout/”;
});
services.AddAuthentication(选项=>
{
options.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
我还想知道AccountController是否需要任何更改?
非常感谢您的帮助?

您是否登录代码如下所示

var claims = new[] 
{ 
    new Claim("name", authUser.Username)
};

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

请参见

中的详细信息初始代码没有什么不同,但现在将其更改为,您提供的解决方案,但错误保持不变Hi Dmitry,很抱歉打扰您我对此完全不熟悉,但我可以共享我的登录名吗()。这是从这行HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal)中断的(identity));再次抛出相同的异常hi@Metin,不,我找不到解决方案。@ashishmokadam我解决了,但我不记得如果你想我怎么解决,我可以与你分享我的启动。cs fileMe我得到这个错误