简体   繁体   English

将未经授权的用户重定向到自定义登录视图 [Asp.net Core 5]

[英]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 5Asp.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.

相关问题 在 ASP.NET 核心中未经授权时重定向到登录 - Redirect to login when unauthorized in ASP.NET Core 如何在 .ASP.NET Core MVC 中使用多个登录页面和未经授权的用户重定向不同的登录页面 - How to use multiple login pages in .ASP.NET Core MVC and unauthorized user redirect different login page ASP.Net Mvc Core Project未经授权的请求不会重定向到“登录”页面 - ASP.Net Mvc Core Project unauthorized request doesn't redirect to Login page 重定向未授权用户asp net - Redirect unauthorized users asp net 当我在 asp.net core mvc 中未经授权时,如何始终返回登录/登录视图? - How to always return login/sign in view when I'm unauthorized in asp.net core mvc? 拦截/停止经过身份验证的用户的ASP.NET Identity未经授权的重定向 - Intercept/stop ASP.NET Identity unauthorized redirect for authenticated users 防止未经授权的登录用户在asp.net Core中进行编辑/删除 - Preventing unauthorized logged users to edit/Delete in asp.net core ASP.NET 核心,更改未经授权的默认重定向 - ASP.NET core, change default redirect for unauthorized Asp.Net Core 未授权登录 - C# - Asp.Net Core Unauthorized login - C# ASP.NET 内核自定义登录设置 - ASP.NET Core custom login setup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM