Jim*_*Jim 27 c# asp.net asp.net-identity asp.net-core-mvc asp.net-core
我试图重定向到ASP.NET MVC6中的另一个登录URL
我的帐户控制器登录方法有一个Route属性来更改网址.
[HttpGet]
[AllowAnonymous]
[Route("login")]
public IActionResult Login(string returnUrl = null)
{
this.ViewData["ReturnUrl"] = returnUrl;
return this.View();
}
Run Code Online (Sandbox Code Playgroud)
当我试图访问一个非正式的页面时,我被重定向到无效的网址,它应该只是
/login但是我得到了http://localhost/Account/Login?ReturnUrl=%2Fhome%2Findex
我已经配置了cookie身份验证路径,如下所示:
services.Configure<CookieAuthenticationOptions>(opt =>
{
opt.LoginPath = new PathString("/login");
});
Run Code Online (Sandbox Code Playgroud)
我添加了一个默认过滤器,以确保默认情况下所有URL都需要身份验证.
services.AddMvc(
options =>
{
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
});
Run Code Online (Sandbox Code Playgroud)
我已经检查过url /login确实加载了登录页面,而/account/login没有按预期加载.
编辑:我已按原样离开路线,(除了更改默认控制器和操作)
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Site}/{action=Site}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
mxm*_*ile 41
随着asp.net core 2.0现在出去,这已更改为:
services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");
Run Code Online (Sandbox Code Playgroud)
有关迁移到2.0的更多信息.而且甚至更多的信息有关迁移从2.0到2.1.
Sta*_*cev 16
因为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";
});
Run Code Online (Sandbox Code Playgroud)
tmg*_*tmg 15
如果选中UseIdentity扩展方法在这里你会发现,它使用IdentityOptions没有CookieAuthenticationOptions,因此您必须配置IdentityOptions:
services.Configure<IdentityOptions>(opt =>
{
opt.Cookies.ApplicationCookie.LoginPath = new PathString("/login");
});
Run Code Online (Sandbox Code Playgroud)
编辑
对于asp.net核心2.0:身份cookie选项不再是IdentityOptions的一部分.检查mxmissile的答案.
您可能还想尝试使用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");
});
Run Code Online (Sandbox Code Playgroud)
添加身份验证服务时,您需要在 startup.cs 中进行配置,尤其是在您使用 cookie 身份验证方案时。
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login");
});
Run Code Online (Sandbox Code Playgroud)
这就是我解决问题的方法,你应该尝试一下......它肯定对你有用
| 归档时间: |
|
| 查看次数: |
31702 次 |
| 最近记录: |