简体   繁体   中英

How to make Login page as a default route in ASP .NET Core 2.1?

I am beginner in ASP .NET Core 2.1 and working on project which is using ASP .NET Core 2.1 with individual authentication. I want to make my login page as my default route instead of Home/Index:

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

Any help how can i change it as ASP .NET Core 2.1 as Login is now used as a razor page instead of MVC Action View.

Use this in ConfigureServices method.

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

then in Configure method

 app.UseMvc(routes =>
        {
            routes.MapRoute(
               name: "default",
               template: "{controller=Home}/{action=Index}/{id?}");

        });

I solve this by using this code in ConfigureServices function (Startup.cs)

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

this may help, i haven't had a need to change default page myself

https://exceptionnotfound.net/setting-a-custom-default-page-in-asp-net-core-razor-pages/

Just use this in your configuration. This will add AuthorizeAttribute to your page

services.AddMvc()
.AddRazorPagesOptions(options =>
{
    options.Conventions.AuthorizePage("/Home/Index");
});

Or change the Default route like this :

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Employees/Index", "");
});

See this page if necessary : https://docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.1

Insert this code to ConfigureServices() in Startup.cs

{
   services.AddMvc().AddRazorPagesOptions(options =>
   {
       //Registering 'Page','route-name'
       options.Conventions.AddPageRoute("/Account/Login", "");
   });
}
  • Remember to remove any route name on "/Account/Login" Action declaration

After along time I solved it. Need add ALLOW for AREAS =>

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorPagesOptions(options =>
        {
            options.AllowAreas = true; //--working after add this line
            options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
        });

Add authorization policy so that application by default asks user for authentication for the pages under abc folder and does not ask for some public pages under abc folder.

services.AddRazorPages().AddRazorPagesOptions(options =>
          {
             options.Conventions.AuthorizePage("/abc");
             options.Conventions.AllowAnonymousToPage("/abc/PublicPage");
           });  

I had also in the same problem with my asp net core project which consists an API(asp net core web API) and a client(asp net core MVC), The problem I have faced when I published my project and tried to host it in my localhost it did not return me the login page which I have configured into the project startup.cs file the endpoint setting the default page I had set was "{controller=Account}/{action=Login}/{id?}" but localhost did not respond with this, after finding solution in the web I found some mechanism to do it but that solution doesn't suit me so I have made trick by my own and hope many net core developer would be helpful with my solution: So the trick is I restore the previous configuration of end point as "{controller=Home}/{action=Index}" and did a little change by removing "/{id?}" from the screen so the actual string was "

{controller=Home}/{action=Index}/{id?}

" and I have changed it to "{controller=Home}/{action=Index}" the logic is when program starts by default it will execute the default endpoint setting so program will take us to the controller which is "Home" and action is "Index" before returning the Index view I have checked a value "UserId" which must create upon login and kept it into a session variable,

var userid = _session.GetString("userid"); if (string.IsNullOrEmpty(userid)) { return RedirectToAction("Login","Account"); }

This is how I solved my problem. The localhost and live publish also running fine, simply clicked view in browser and it returns the login page as my requirement. Hope it helps. Thanks

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