0

ログインページにリダイレクトされるのではなく[承認]されたコントローラアクションを要求すると、401エラーが発生します。.netコアMVCを取得できない/ 401/Account/Login

これは、IIS Expressで動作するアイデンティティテンプレートを使用した.netコアmvc​​アプリケーションです。

私はprogram.csからアプリケーションを実行すると、ログインへのリダイレクトは正常に動作します。 Cookie認証で、設定/サービスの両方の/ Account/Loginリダイレクトセクションを使用し、このリダイレクトを実行するようにIdentityを設定するという明確な指示が追加されました。

私はそれを動作させることができません。以下は私の起動クラスがあり、私はそれが

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'ユーザー/要求はあなたはそれが動作するように失敗した後、私は唯一のトラブルシューティングを開始し、すべてのインターネット –

+0

をauthenticated.'ではありません'を意味します。明示的にリダイレクトを設定した後も、リダイレクトは行われません。私がconfigureに追加しているのは、401のリダイレクトを使う明白な指示です。これは、スタンドアローンを実行しているときにうまくいきます。 IISで失敗します。あなたはブラハーを読んでいますか? – Josiah

答えて

7

私はこの同じ問題を一晩中持ち、解決策を見つけることができませんでした。 Kestrelから直接サイトを実行すると、リダイレクトはうまくいきましたが、IISやIIS Expressではリダイレクトされませんでした。そのページは白いページになります。

Identity Gitに投稿した後、テンプレートが1.1.0ではなく、1.0.1のフレームワークで動作するように設定されていることがわかりました。私は1.1.0を使用するように更新し、すべてのNugetパッケージを1.1.0に更新し、IISとIIS Expressで正しくリダイレ​​クトしています。

パッケージが不具合を「修正」しているのか、1.1.0で修正された1.0.1の問題だったのか分かりません。

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

+0

命を救った!ありがとう –

+0

すべてをコアコア1.1にアップグレードした後、問題は解決しませんでした。 – Josiah

1

アイデンティティを表現する?:IISで動作するように変更する必要がありますと、自動的にクッキー認証を追加します。設定にもう一度追加します。

両方の自動プロパティを設定しているので、2つのミドルウェアがリダイレクトを試みています。その動作は "未定義"です(ここでundefined == " )。

+1

号を獲得TânNguyễ[email protected] – Josiah

関連する問題