C# ASP.NET核心,更改未经授权的默认重定向
我正在尝试重定向到ASP.NET MVC6中的其他登录url 我的帐户控制器登录方法有一个C# ASP.NET核心,更改未经授权的默认重定向,c#,asp.net,asp.net-core,asp.net-identity,asp.net-core-mvc,C#,Asp.net,Asp.net Core,Asp.net Identity,Asp.net Core Mvc,我正在尝试重定向到ASP.NET MVC6中的其他登录url 我的帐户控制器登录方法有一个路由属性来更改url [HttpGet] [AllowAnonymous] [Route("login")] public IActionResult Login(string returnUrl = null) { this.ViewData["ReturnUrl"] = returnUrl; return this.View(); } 当我试图访问一个未经筛选的页面时,我被重定向到无效
路由
属性来更改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/login
是否确实加载了登录页面,而/account/login
没有加载,正如预期的那样
编辑:除了更改默认控制器和操作外,我已保留路由不变
如果选中
UseIdentity
扩展方法,您会注意到它使用的是IdentityOptions
而不是CookieAuthenticationOptions
,因此您必须配置IdentityOptions
:
services.Configure<IdentityOptions>(opt =>
{
opt.Cookies.ApplicationCookie.LoginPath = new PathString("/login");
});
services.Configure(opt=>
{
opt.Cookies.applicationcokie.LoginPath=新路径字符串(“/login”);
});
编辑
对于asp.net core 2.0:
标识cookie选项不再是标识选项的一部分。检查mxmissile。现在退出
asp.net core 2.0
,这已更改为:
services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");
更多关于。在从2.0迁移到2.1时。您可能还想尝试使用
StatusCodePages
:
app.UseStatusCodePages(async context => {
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Unauthorized ||
response.StatusCode == (int)HttpStatusCode.Forbidden)
response.Redirect("/Error/Unauthorized");
});
更新:
从dotnetcore2.1.x开始,Identity是从SDK构建的。
若要对@mxanse进行co--sign,可以指定路径。要实现一个技巧路径,请结合高级路由或重定向。我不建议在实际示例中使用Serj Sagan解决方案。如果不是针对不同类型用户使用的实际应用程序,这在开发时可能会产生误导,那么这将非常有效。让我们看看下面的场景
services.AddMvcCore(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.AddRazorViewEngine()
.AddAuthorization()
.AddJsonFormatters();
由于
asp.net core 2.0
如果您使用不带标识的cookie:
app.UseAuthentication();
// If you don't want the cookie to be automatically authenticated and assigned HttpContext.User,
// remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/LogIn";
options.LogoutPath = "/Account/LogOff";
});
添加身份验证服务时,尤其是在使用cookie身份验证方案时,您需要在startup.cs中对此进行配置
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login");
});
这就是我解决问题的方法,你应该试试看……它肯定对你有用。你能在
Configure()
中显示你的路由配置吗?@juunas:我已经在问题中添加了路由,你是否使用asp.net核心标识?@tmg,是的,我是,但是有了这个支持EF6的插件,我不认为重定向会受到EF版本的影响,但我可能错了。我仍然在使用Microsoft.AspNetCore.Builder中的app.UseIdentity()
,我在这里也找到了它,谢谢,效果很好。(这篇文章似乎没有提供问题的答案。请编辑你的答案,或者只是将其作为对其他参考答案的注释发布)。这对Core 3.0仍然有效,工作非常完美!谢谢你挽救了这一天:D
app.UseAuthentication();
// If you don't want the cookie to be automatically authenticated and assigned HttpContext.User,
// remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/LogIn";
options.LogoutPath = "/Account/LogOff";
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login");
});