gpt4 book ai didi

asp.net - 没有配置身份验证处理程序来处理方案 : Automatic

转载 作者:行者123 更新时间:2023-12-03 11:03:15 31 4
gpt4 key购买 nike

我在以前工作的应用程序上用 RC 更新了 ASP.NET 5 框架 beta-8 包。在我让它运行后,启动过程中出现下一个错误:

InvalidOperationException: No authentication handler is configured to handle the scheme: Automatic Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d__12.MoveNext()


var defaultPolicy =
new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();

services.AddMvc(setup =>
{
setup.Filters.Add(new AuthorizeFilter(defaultPolicy)); // Error occurs here
});

如果有人遇到类似的问题,我会很感激您对可能出现问题的想法或解决方案。也感谢对​​此异常的解释。

Startup.cs
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using SuperUserMVC.Configuration;
using SuperUserMVC.Extensions;
using SuperUserMVC.GlobalModules;
using System;

namespace SuperUserMVC
{
public class Startup
{
public IConfigurationRoot Configuration { get; set; }

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json");

Configuration = builder.Build();
}

public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettingsBase>(Configuration.GetSection("AppSettingsBase"));
services.Configure<ConnectionString>(Configuration.GetSection("ConnectionString"));

services.AddSqlServerCache(cache =>
{
cache.ConnectionString = Configuration.Get<string>("ASPState:ConnectionString");
cache.SchemaName = Configuration.Get<string>("ASPState:Schema");
cache.TableName = Configuration.Get<string>("ASPState:Table");
});

services.AddSession(session =>
{
session.IdleTimeout = TimeSpan.FromMinutes(120);
});

// Only allow authenticated users.
var defaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();

// Add MVC services to the services container.
services.AddMvc(setup =>
{
setup.Filters.Add(new AuthorizeFilter(defaultPolicy));
});

var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacModule());
builder.Populate(services);

var container = builder.Build();

return container.Resolve<IServiceProvider>();
}

public void Configure(IApplicationBuilder app, IHttpContextAccessor httpContextAccessor)
{
// Catch unhandled exception in pipeline.
bool isProductionEnvironment = Configuration.Get<bool>("environmentVariables:isProductionEnvironment");
app.UseCustomUnhandledException(isProductionEnvironment, Configuration.Get<string>("defaultErrorPagePath"));

// Log requests.
app.UseVisitLogger(isProductionEnvironment);

// Session must be used before MVC routes.
app.UseSession();

// Configure the HTTP request pipeline.
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "Cookies";
options.LoginPath = new PathString("/Account/Login/");
options.AccessDeniedPath = new PathString("/Account/Forbidden/");
options.CookieName = "MyCookie";
options.AutomaticAuthenticate = true;
options.SessionStore = new MemoryCacheSessionStore();
});

AutoMapperInitializer.Init();
app.UseStaticFiles();

// Route configuration.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "AreaDefault",
template: "{area:exists=Demo}/{controller=Home}/{action=Index}/{id?}"
);

routes.MapRoute(
name: "Default",
template: "{controller=Home}/{action=Index}/{id?}"
);
});
}
}
}

最佳答案

尝试设置 options.AutomaticChallenge = true;在您的 cookie 选项中,它应该可以工作。
options.AutomaticAuthentication被拆分为 options.AutomaticAuthenticateoptions.AutomaticChallenge .如果最后一个留给false ,因为没有身份验证中间件处理授权过滤器应用的质询,所以抛出异常。

关于asp.net - 没有配置身份验证处理程序来处理方案 : Automatic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825058/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com