【发布时间】:2017-02-08 05:32:52
【问题描述】:
我正在尝试重定向到 ASP.NET MVC6 中的不同登录 url
我的帐户控制器登录方法有一个Route 属性来更改url。
[HttpGet]
[AllowAnonymous]
[Route("login")]
public IActionResult Login(string returnUrl = null)
{
this.ViewData["ReturnUrl"] = returnUrl;
return this.View();
}
当尝试访问未经授权的页面时,我被重定向到无效的 url,它应该只是
/login但我得到了http://localhost/Account/Login?ReturnUrl=%2Fhome%2Findex
我已经配置了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 没有按预期加载。
编辑:我已将路线保持原样(除了更改默认控制器和操作)
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Site}/{action=Site}/{id?}");
});
【问题讨论】:
-
你能在
Configure()中显示你的路由配置吗? -
@juunas:我已经为问题添加了路线
-
你在使用 asp.net-core-identity 吗?
-
@tmg,是的,我是,但是有了这个插件github.com/mrahhal/MR.AspNet.Identity.EntityFramework6 来支持 EF6,我认为重定向不会受到 EF 版本的影响,但我可能是错的。我仍在使用来自
Microsoft.AspNetCore.Builder的app.UseIdentity()
标签: c# asp.net asp.net-core asp.net-identity asp.net-core-mvc