我正在try 重定向到ASP中的其他登录url.净MVC6

我的帐户控制器登录方法有一个Route属性来更改url.

[HttpGet]
[AllowAnonymous]
[Route("login")]
public IActionResult Login(string returnUrl = null)
{
    this.ViewData["ReturnUrl"] = returnUrl;
    return this.View();
}

当我试图访问一个不受限制的页面时,我被重定向到无效的url,它应该只有/login,但我得到了

我已将cookie身份验证路径配置如下:

services.Configure<CookieAuthenticationOptions>(opt =>
{
    opt.LoginPath = new PathString("/login");
});

我添加了一个默认的过滤,以确保默认情况下所有URL都需要身份验证.

services.AddMvc(
    options =>
    {
        options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
    });

我已经判断过,url /login确实加载了登录页面,而/account/login没有加载,这与预期的一样.

edit:我保留了路由(除了更改默认控制器和操作)

app.UseMvc(routes =>
{
    routes.MapRoute(
      name: "default",
      template: "{controller=Site}/{action=Site}/{id?}");
});

推荐答案

如果选中UseIdentity扩展方法here,您会注意到它使用的是IdentityOptions而不是CookieAuthenticationOptions,因此您必须配置IdentityOptions:

services.Configure<IdentityOptions>(opt =>
{
    opt.Cookies.ApplicationCookie.LoginPath = new PathString("/login");
});

Edit

对于asp.net core 2.0:

Asp.net相关问答推荐

如何以编程方式设置单元格 colored颜色 epplus?

从数据库中检索数据的最快方法

系统日期时间?与 System.DateTime

iframe.readyState 在 chrome 中不起作用

在构建时自动停止/重新启动 ASP.NET 开发服务器

对布尔查询字符串参数使用true或1

Web.config 导致被组策略阻止错误

Jquery asp.net 按钮单击事件通过 ajax

如何在asp.net mvc的html.actionlink中调用javascript函数?

MVC5 身份验证中的......与主域之间的信任关系失败

在 ASP.NET MVC 中使用 Razor 创建可重用的 HTML 视图组件

在 aspnetcore (netcoreapp1.0) 中替代 System.Web.Security.Membership.GeneratePassword

使用 Ninject OWIN 中间件在 OWIN 启动中注入 UserStore

web.config 中与 targetFramework 相关的配置错误

回发不适用于 aspx 页面作为默认文档

如何判断本地主机

ASP.NET 发布try 复制不存在的文件

URL 路由、图像处理程序和潜在危险的 Request.Path 值

如何使用 jquery 设置单选按钮 Select 的值

为什么 C# 对于 CSC 文件会遇到此错误?