ASP.NET核心授权重定向到错误的URL

时间:2016-12-07 12:34:04

标签: redirect routes asp.net-core asp.net-core-mvc .net-core

我正在尝试使用以下路由映射运行Web应用程序:

<?php



$a = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));


$b = Array('0' => Array('0' => 7,'1' => 5),'1' => Array('0' => 3,'1' => 2));

$sumArray = array();

$c = array();

for($i=0;$i<2;$i++) {
 for($j=0;$j<2;$j++) 
 { 
    $c[$i][$j]=0; 
    for($k=0;$k<2;$k++) 
        { $c[$i][$j]=$c[$i][$j]+($a[$i][$k]*$b[$k][$j]); 
    } 
} 
} 


echo "<pre/>";
print_r($c);
?>

如果用户未经过身份验证并尝试访问具有AuthorizeAttribute的操作,则应将用户重定向到默认登录URL(如上所示)。但是用户被重定向到&#34; /帐户/登录&#34;而不是&#34; / WoL / Account / Login&#34;。如果用户未经过身份验证,如何将用户重定向到&#34; / WoL / Account / Login&#34;?我已配置以下Cookie身份验证:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                "default",
                "WoL/{controller=Account}/{action=Login}/{id?}");
        });

2 个答案:

答案 0 :(得分:4)

@Dmitry的答案在ASP.NET Core 3.1中不再起作用。根据您可以找到的here文档,您必须将以下代码添加到ConfigureServices中:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest)
   .AddRazorPagesOptions(options =>
   {
       options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
       options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

    services.ConfigureApplicationCookie(options =>
    {
       options.LoginPath = $"/Identity/Account/Login";
       options.LogoutPath = $"/Identity/Account/Logout";
       options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
     });

答案 1 :(得分:3)

这对我有用(在Startup.ConfigureServices中):

services.AddIdentity<User, UserRole>(options => 
{
    options.Cookies.ApplicationCookie.LoginPath = new PathString("/Admin/Account/Login");
});