ASP.Net core MVC6 Redirect to Login when not authorised(未经授权时,ASP.Net core MVC6 Redirect to Login)
问题描述
我正在使用 ASP.Net core MVC 6,如果用户未通过身份验证,我会尝试将其重定向到登录页面.
I am using ASP.Net core MVC 6, I am trying to get the user redirected to the login page if they are not authenticated.
我似乎无法让它工作,目前用户只是得到一个空白页.
I cant seem to get it to work, currently the user just gets a blank page.
下面是我在 Startup.cs 中的 ConfigureServices 方法
Below is my ConfigureServices method in Startup.cs
public void ConfigureServices(IServiceCollection services) {
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
);
services.AddIdentity<ApplicationUser, IdentityRole>(options => {
// configure identity options
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 7;
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
options.Cookies.ApplicationCookie.AutomaticChallenge = true;
options.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
// User settings
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
推荐答案
我自己只是在纠结这个问题,我得出的结论是,在最新版本的Microsoft.AspNetCore.Identity.EntityFrameworkCore" 依赖.
I was just wrestling with this myself and I've come to the conclusion that there seems to be an issue in the latest version of the "Microsoft.AspNetCore.Identity.EntityFrameworkCore" dependency.
我最初使用的是 1.1.0 版本,但经过大量调试、中间件日志记录等,我得出的结论是我没有做错任何事情.我检查了:
I was originally using version 1.1.0 but after lots of debugging, owin middleware logging etc, I came to the conclusion that I wasn't doing anything wrong. I checked:
- Authorize 属性起作用并阻止了请求
添加如下事件处理程序 (OnRedirectToLogin) 以验证重定向 URL(这仅用于调试)
- Authorize attribute worked and blocked the request
Added event handlers (OnRedirectToLogin) as below to verify the redirect URL (this was only for debugging)
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = evt => {
evt.Response.Redirect(evt.RedirectUri); // this url is correct, but the redirect never happens!??
return Task.FromResult(0);
}
};
分辨率:我将包回滚到版本 1.0.1,然后重定向按预期启动 - 到 LoginPath 设置中 Startup.cs 中定义的 URL
The resolution: I rolled back my package to the version 1.0.1 and then the redirects kicked in as expected - to the URL defined in Startup.cs in the LoginPath setting
options.Cookies.ApplicationCookie.LoginPath = new PathString("/Auth/Login");
澄清一下,这个版本有效:Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.1"
我将向 ASPNETCORE 团队提出一个关于 1.1.0 版本的错误以进行调查.
I'm going to raise a bug with the ASPNETCORE team for investigation as regards to the 1.1.0 version.
这篇关于未经授权时,ASP.Net core MVC6 Redirect to Login的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未经授权时,ASP.Net core MVC6 Redirect to Login
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30