Search code examples
c#asp.net-coreasp.net-core-mvcasp.net-identityasp.net-core-2.0

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 :)


Solution

  • Your question is unclear. I'm reasonably sure that you aren't actually wanting the login page to be your "start page" (i.e. 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"