[英]Redirect unauthorized users to custom login view [Asp.net Core 5]
I use Identity
in an Asp.net Core 5
project targeted .Net 5
.我在针对
.Net 5
的Asp.net Core 5
项目中使用Identity
。 As you know when make a Controller
as Authorize
any user try to access any view
in the controller the Identity
will redirect him to the /Identity/Account/Login
razor page, so I created my own login controller with views. As you know when make a
Controller
as Authorize
any user try to access any view
in the controller the Identity
will redirect him to the /Identity/Account/Login
razor page, so I created my own login controller with views.
The question: I wanna the unauthorized
users to redirected to my custom login
view, how can I do that?问题:我希望
unauthorized
的用户重定向到我的自定义login
视图,我该怎么做?
What I tried: In ConfigureServices
method in Startup
class I added this piece of code:我尝试了什么:在
Startup
class 的ConfigureServices
方法中,我添加了这段代码:
services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme )
.AddCookie( options =>
{
options.LoginPath = "/Login";
} );
services.AddControllersWithViews();
But still not working the users still redirected to /Identity/Account/Login
and I wanna them riderected to my custom login view
.但仍然无法正常工作,用户仍然被重定向到
/Identity/Account/Login
,我希望他们被重定向到我的自定义login view
。
In my case I replaced my old code with:就我而言,我将旧代码替换为:
services.ConfigureApplicationCookie( options =>
{
options.LoginPath = "/Login";
} );
Thanks to Martin Costello感谢马丁·科斯特洛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.