简体   繁体   English

如何在 ASP.NET CORE 5.0 MVC 中将登录设置为默认路由

[英]How to make Login as Default route in ASP.NET CORE 5.0 MVC

I am trying to load Login Page in the first start when application is load.我试图在加载应用程序时在第一次启动时加载登录页面。 So far What I try到目前为止我尝试了什么

Configure配置

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    //pattern: "{area=Manager}/{controller=Ticket}/{action=Index}/{id?}");
                    pattern: "{area=Manager}/{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

ConfigureServices配置服务

services.AddMvc().AddRazorPagesOptions(options => {
                options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

This method works fine, but after user click Login button nothing happened.此方法工作正常,但用户单击登录按钮后没有任何反应。 The Login Page in againg loaded.再次加载中的登录页面。 SO far I check couple of post here but whatever I check it is releted to .NET CORE 2.0 and CORE 2.1 Any idea what is the problem here?到目前为止,我在这里检查了几个帖子,但无论我检查什么,它都与.NET CORE 2.0CORE 2.1相关,知道这里有什么问题吗?

Default Routes are supposed to redirect people to a common page (like index.* or start.*, etc.) where the user is able to navigate from and do their stuff.默认路由应该将人们重定向到一个公共页面(如 index.* 或 start.* 等),用户可以从中导航并做他们的事情。

Setting up a login page for that seems wrong to me, because you are not able to do anything else there, but to login and authenticate.为此设置登录页面对我来说似乎是错误的,因为您无法在那里做任何其他事情,只能登录和验证。

You stated, that you want to always show the login page before anything else happens, then I guess your application is totally secured and no action or function is available without a valid login.您说过,您希望始终在发生其他任何事情之前显示登录页面,那么我猜您的应用程序是完全安全的,没有任何操作或 function 在没有有效登录的情况下可用。

If this is the case, then simply require an Authorization on every single page, that redirects you then automatically to your login screen.如果是这种情况,那么只需在每个页面上都需要一个授权,然后自动将您重定向到您的登录屏幕。 You can use [Authorize] attribute for that or even configure it globally inside the Configure method: endpoints.MapRazorPages().RequireAuthorization();您可以为此使用 [Authorize] 属性,甚至可以在 Configure 方法中对其进行全局配置: endpoints.MapRazorPages().RequireAuthorization();

hint: haven't checked this if it works, some searches against "asp.net mvc global authorization" might help here提示:还没有检查它是否有效,一些针对“asp.net mvc 全局授权”的搜索可能会有所帮助

This solution work for me这个解决方案对我有用

ConfigureServices配置服务

 services.AddMvc().AddRazorPagesOptions(options => {
                options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
            });

Configure配置

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{area=Manager}/{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages().RequireAuthorization();
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM