2015-11-20 81 views
21

我更新了以前工作的应用程序的RC 5的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?}" 
       ); 
      }); 
     } 
    } 
} 
+0

我刚刚开始用asp.net开发最后一个测试版(创建了一个新项目,然后对其进行了一些改进),并且在更新到RC之后也出现问题,因为它们已经更改了各种BASE事物。不幸的是,我没有找到任何描述,如何改变现有的项目兼容。所以...然后我从头开始重新创建我的项目,以完全兼容。由于RC是由MS支持生产的,我认为(希望如此)将来不会有这种变化(2016年第1季度发布)。 – FredyWenger

回答

28

尝试在你的cookie的选项来设置options.AutomaticChallenge = true;,它应该工作。

options.AutomaticAuthentication被分成options.AutomaticAuthenticateoptions.AutomaticChallenge。如果最后一个留给false,则会抛出异常,因为没有身份验证中间件处理由授权筛选器应用的挑战。

+0

再次感谢你,这解决了我的问题! :) – msmolcic

+4

如果您不使用Cookies,该怎么办? –

+1

@ AlexHopeO'Connor,我直接将其设置为Google身份验证选项 – SiberianGuy

0

虽然这是很有诱惑力的地方多的startup.cs文件中我们的配置设置,好像是做事情的首选方式是设置app.UseCookieAuthentication() - SANS选项 - 在startup.cs文件中,然后把所有的“选项'和其他细节在一个单独的文件中。

有点像我们在做什么,Global.asax文件如何指向Asp.Net vBefore中的App_Start文件夹文件。

startup.cs中尝试配置EF/Sql时,我遭受了类似的痛苦,并且通过移动startup.cs之外的所有“选项”,情况变得更好。

ALSO:注意Fredy Wenger对您的问题的评论,指出了从v-8beta到v-RC1-final的许多命名空间的“重命名”。

20

把它放在Configure方法上。

 app.UseIdentity(); 
+0

我认为你的意思是'Configure',而不是'ConfigureServices'。 – Peter

+0

我有这条线,但仍然收到错误... –

49

希望这将帮助别人,因为我刚刚花了很多时间来处理,即使我已成立AutomaticChallenge = true此错误。

原来,如果您在app.UseMvc(routes => ...)之后放置app.UseIdentity();,您将会得到相同的错误。现在我知道答案很明显。这是因为所有这些中间件都是按照您添加的顺序进行的。

这将导致“无验证处理程序被配置”错误:

public void Configure(...) 
    { 
     app.UseMvc(routes => { routes.MapRoute(...) };); 

     app.UseIdentity(); 
    } 

这不会导致错误:

public void Configure(...) 
    { 
     app.UseIdentity(); 

     app.UseMvc(routes => { routes.MapRoute(...); }); 
    } 
+2

谢谢。这似乎适用于Uapp.UseOpenIdConnectAuthentication()和app.UseCookieAuthentication() – tjrobinson

+1

一年后仍然适用于我。 Thx – Mariusz

+0

我的错误是这种调用顺序(因为我正在嘲笑集成测试认证)。我永远不会猜到它!谢谢! – Vetras

2

另一种可能性是缺少配置

以下设置
app.UseCookieAuthentication(); 
6

问题wa通过确保cookie方案在任何被引用的地方被一致地命名来解决我的问题。例如:

public void ConfigureServices(IServiceCollection services) 
{ 
    // if using IdentityServer4 
    var builder = services.AddIdentityServer(options => 
    { 
     options.AuthenticationOptions.AuthenticationScheme = Constants.DefaultCookieAuthenticationScheme; 
     ... 
    }) 

    services.AddIdentity<MyUser, IdentityRole>(options => 
    { 
     options.Cookies.ApplicationCookie.AuthenticationScheme = Constants.DefaultCookieAuthenticationScheme; 
     ... 
    } 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    ... 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationScheme = Constants.DefaultCookieAuthenticationScheme, 
     AutomaticAuthenticate = false, 
     AutomaticChallenge = true 
    }); 
} 

而且当与认证中间件进行交互时。例如: -

await HttpContext.Authentication.SignInAsync(Constants.DefaultCookieAuthenticationScheme, cp); 
+0

在实现了有关“options.AutomaticAuthentication分为options.AutomaticAuthenticate和options.AutomaticChallenge”的第一个答案后,我仍然收到相同的错误,但是将大写“Cookies”替换为小写(因为它是拼写在另一个地方),我的应用程序开始工作!非常感谢你! –

2

如果使用app.UseIdentity();和其他一些登录中间件如UseFacebookAuthentication确保app.UseFacebookAuthentication()之后app.UseIdentity();

相关问题