0

Wenn ich eine Controller-Aktion anfordern, die [Autorize] eingerichtet ist anstatt auf die Anmeldeseite umgeleitet werden, erhalte ich einen Fehler 401.Kann .net Kern MVC um 401 zu/Konto/Login umleiten

Dies ist eine .net-Core-MVC-Anwendung, die die auf IIS Express ausgeführte Identitätsvorlage verwendet.

Wenn ich die App von program.cs ausführen, funktioniert die Umleitung zum Login gut. Ich habe explizite Anweisungen für die Cookie-Authentifizierung hinzugefügt, um die/Account/Login-Umleitung sowohl für den Konfigurations- als auch für den Serviceteil zu verwenden und Identity für diese Umleitung zu konfigurieren.

Ich kann es nicht zum Laufen bringen. Unten ist meine StartUp-Klasse, was sollte ich ändern, damit es in IIS Express funktioniert ?:

public class Startup 
{ 
    private MapperConfiguration _mapperConfiguration { get; set; } 

    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

     if (env.IsDevelopment()) 
     { 
      // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 
      builder.AddUserSecrets(); 
     } 

     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 

     _mapperConfiguration = new MapperConfiguration(cfg => 
     { 
      cfg.AddProfile(new AutoMapperProfileConfiguration()); 
     }); 
    } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 


     services.AddIdentity<ApplicationUser, IdentityRole>(
      option => { 
       option.Cookies.ApplicationCookie.LoginPath = "/Account/Login"; 
       option.Cookies.ApplicationCookie.AutomaticChallenge = true; 
       option.Cookies.ApplicationCookie.AutomaticAuthenticate = true; 
      }) 
      .AddEntityFrameworkStores<ApplicationDbContext>(); 

     services.AddDataProtection(); 

     services.AddMvc(); 
     services.AddSignalR(); 

     // Add application services. 
     services.AddTransient<IEmailSender, AuthMessageSender>(); 
     services.AddTransient<ISmsSender, AuthMessageSender>(); 
     services.Configure<AuthMessageSenderOptions>(Configuration); 
     services.Configure<IISOptions>(options => options.AutomaticAuthentication = true); 
     services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper()); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context, RoleManager<IdentityRole> roleManager) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 



     app.UseStaticFiles(); 

     app.UseIdentity(); 

     // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 

     //app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationScheme = "MyCookies", 
      SlidingExpiration = true, 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true, 
      LoginPath = new PathString("/Account/Login") 
     }); 
     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
      routes.MapRoute(
       name: "index", 
       template: "{controller=Home}/{id?}", 
       defaults: new { action = "Index" }); 
     }); 
     app.UseSignalR(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseDatabaseErrorPage(); 
      app.UseBrowserLink(); 
     } 
     else 
     { 
      app.UseExceptionHandler("/Home/Error"); 
     } 

     MyDbInit.Init(context, roleManager); 

    } 
} 
+0

'401' bedeutet' User/Anfrage nicht –

+0

@ TânNguyễn authenticated.' wird Sie alle internets – Josiah

Antwort

7

Ich hatte das gleiche Problem die ganze Nacht und konnte keine Lösung finden. Das Ausführen der Site direkt von Kestrel wurde ordnungsgemäß umgeleitet, aber durch IIS oder IIS Express würde es einfach nicht umgeleitet - es würde auf eine weiße Seite gehen.

Nach der Veröffentlichung im Identity Git merkte ich, dass meine Vorlage so konfiguriert wurde, dass sie unter 1.0.1 des Frameworks ausgeführt wurde, nicht 1.1.0. Ich habe es aktualisiert, um 1.1.0 zu verwenden und alle Nuget-Pakete auf 1.1.0 zu aktualisieren, und jetzt wird es in IIS und IIS Express richtig umgeleitet.

Ich bin mir nicht sicher, ob das Paket "repariert" etwas, das verschraubt war, aktualisiert, oder wenn dies einfach ein Problem mit 1.0.1 war, das in 1.1.0 behoben wurde.

https://blogs.msdn.microsoft.com/webdev/2016/11/16/announcing-asp-net-core-1-1/

+0

Leben gerettet! Danke –

+0

nach dem Upgrade alles auf Netzkern 1.1 ging das Problem weg. – Josiah

1

Identität fügt Cookie-Authentifizierung automatisch hinzu. Sie fügen es ein zweites Mal in Konfigurieren hinzu.

Wenn Sie die zweite Instanz hinzufügen, legen Sie beide automatischen Eigenschaften fest, also versuchen nun zwei Middlewares die Umleitung, und dieses Verhalten ist "undefiniert" (wo undefined == "Wird ernsthaft Dinge vermasseln")).

+1

Nr gewinne ich die Fehlersuche nur begonnen, nachdem es an die Arbeit gescheitert. Auch nach dem expliziten Setzen der Weiterleitungen wird nicht umgeleitet. Was ich in der Konfiguration hinzufüge, ist die ausdrückliche Anweisung, eine Weiterleitung auf 401 zu verwenden. Dies funktioniert gut, wenn ich eigenständig arbeite. Es schlägt in IIS fehl. Liest du überhaupt Brah? – Josiah