How to make a login page a start page in an ASP.NET MVC web application?

I want to create a login page that opens when the ASP.NET MVC web application starts. I also want to automatically redirect the user to the main page / index after a successful login.

In addition, there is a Register button on the login page, which redirects to the registration page, I want the registration page to be redirected to the Home / Index after successful registration.

+4
source share
3 answers

You do not want to log in as your homepage. This is not a good design. Mostly because after logging in and logging in https://yoursite.comto the browser you do not want to display the login page again.

Instead, you just need to apply [Authorize] to the home controller.

[Authorize]
public class HomeController : BaseController
{
  // ...
}

Or Global Filter

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());
    }
}

If a user accesses your homepage, he will be redirected to the login page first with ReturnUrl in QueryString .

For instance, https://yoursite/Account/Login?ReturnUrl=%2f

Make sure you set the loginUrl login page in web.config.

  <system.web>
    ...
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880"/>
    </authentication>
  </system.web>
+9
source

You have two options: 1. Register the default route on the login page

public static void RegisterRoutes(RouteCollection routes)
    {

        routes.MapRoute(
            "Default", 
            "{controller}/{action}", 
            new { controller = "Home", action = "Login"} 
        );

    }
  1. Home/Index, , , , , , login
+3

- . , . : 1.app.config 2.route.config

Route Config, url: -

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new 
            { 
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional
            }
        );
    }
}

, MVC . " " . . ( URL). , URL-, , MVC:

~/{controller}/{action}

URL- : routes.MapRoute(  : "SiteMap",  url: "sitemap",  defaults: new {controller = "Static", action = "SiteMap", id = UrlParameter.Optional},  namespaces: new string [] { "name.Web.Controllers" } );

To set the default page: the best way is to change the route. The default route (defined in your App_Start) sets / Home / Index

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);

as the default landing page. You can change this like any route you want.

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Sales", action = "ProjectionReport", 
        id = UrlParameter.Optional }
);

I hope you have a clear idea about this

0
source

Source: https://habr.com/ru/post/1599499/


All Articles