简体   繁体   中英

How do I set the start page as the login page in asp.net core 2.0

Hi guys I don't have much knowledge of routing except for the basics.

I am trying to make the start page as the login page in ASP.Net Core 2.0.

This is my configuration in Startup.cs:

services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings  
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
            options.LoginPath = "/Account/Login"; 
            options.LogoutPath = "/Account/Logout"; 
            options.AccessDeniedPath = "/Account/AccessDenied"; 
            options.SlidingExpiration = true;
        });

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{Controller=Account}/{action=Login}/{id?}");
    });

My understanding is that this should take me to the Login Page. However /Identity/Account/Login takes me to the login page and the above doesn't work.

Need Direction. Thank you :)

Your question is unclear. I'm reasonably sure that you aren't actually wanting the login page to be your "start page" (ie the default page if you just go to the domain with no path), but rather to have your "start page" require authentication and automatically redirect to your login page when not authenticated.

There, it seems you're simply misunderstanding how routing works. The URLs specified for your application cookie settings don't magically make your pages appear there. It's simply statically telling the framework where to redirect to for these conditions. You still have to actually have something respond to that particular route, or you'll get a 404. By default the Identity pages have routes under /Identity . If you want to change this, you'll need to scaffold them into your project and then either move them into your main Pages folder at the project-level or add an explicit route with the @page declaration:

@page "/Account/Login"

Based on the URL you mentioned (/Identity/Account/Login), "Identity" must be the Area. And as the Area is not defined in default route, it is redirecting to a wrong URL. All you need is to put the Area in Default Route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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