0

當我請求一個控制器操作是[授權]裝飾,而不是被重定向到登錄頁面時,我收到401錯誤。無法獲得.NET核心MVC重定向401到/帳戶/登錄

這是一個使用在IIS Express上運行的身份模板的.net核心mvc應用程序。

當我從program.cs運行應用程序重定向到登錄工作正常。 我已經添加了明確的指示,以便cookie身份驗證使用配置和服務部分的/帳戶/登錄重定向,以及配置身份以執行此重定向。

我不能得到它的工作。以下是我的StartUp類,我應該更改哪些內容以使其在IIS Express中工作?:

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'意味着'用戶/請求沒有authenticated.' –

+0

@TânNguyễn你贏了所有的互聯網絡 – Josiah

回答

7

我整晚都有同樣的問題,無法找到解決方案。直接從Kestrel運行站點重定向好,但通過IIS或IIS Express它不會重定向 - 它會轉到白頁。

在發佈到Identity Git上後,我意識到我的模板已設置爲在框架的1.0.1下運行,而不是1.1.0。我將其更新爲使用1.1.0並將所有Nuget軟件包更新至1.1.0,現在它正確地在IIS和IIS Express中重定向。

我不確定程序包是否更新了「固定」的東西,如果這只是1.0.1版固定在1.1.0中的問題。

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

+0

拯救生命!感謝 –

+0

將所有內容升級到net core 1.1後,問題就消失了。 – Josiah

1

Identity會自動添加Cookie身份驗證。您將在Configure中再次添加它。

當你添加第二個實例時,你設置了兩個自動屬性,所以現在兩個中間件都試圖做重定向,並且這種行爲是「未定義的」(其中undefined ==「要認真地搞砸了」 )。

+1

號我後,纔沒能成功啓動故障排除。即使明確設置重定向後,它也不會重定向。我在配置中添加的內容是在401上使用重定向的明確方向。在獨立運行時,此工作正常。它在IIS中失敗。你甚至讀過胸罩嗎? – Josiah