9

I want to make a login page that opens when I start up the ASP.NET MVC web application. I also want to automatically redirected the user to the Home/Index page after a successful login.

Furthermore the login page has a Register button which is redirected to the register page, I want the register page to be redirected to the Home/Index after successful registration.

CuriousPenguin
  • 87
  • 1
  • 14
TheoWallcot12
  • 109
  • 2
  • 2
  • 4
  • so where is problem, and what is question? – Grundy Jul 24 '15 at 11:50
  • Start with the sample application that comes along with Visual studio. You don't need a single code to write on what you are looking for. Go to VS > new > mvc and you will get your answer. – codebased Jul 24 '15 at 11:51
  • 1
    Make `Home/Index` your default, and add the `[Authorize]` attribute to it (and to all other controller methods except Login and Register). The in the POST methods for Login and Register methods, redirect to Home/Index –  Jul 24 '15 at 11:51
  • the problem is that my startup page is Home/Index and I want it to be Login. – TheoWallcot12 Jul 24 '15 at 11:52
  • Then change it in `RouteConfig.RegisterRoutes` – Ric Jul 24 '15 at 11:52
  • visit http://stackoverflow.com/questions/14426526/how-to-set-default-controller-in-asp-net-mvc-4-mvc-5 – Udit Sharma Jul 24 '15 at 11:54
  • Thanks Stephen Muecke. I think your answer is better than my proposal because if I set the login page as startup page , someone would be able to enter the another application pages only typing their url. – TheoWallcot12 Jul 24 '15 at 11:55
  • @TheoWallcot12, Personally I would not redirect to Home/Index after Login. Its rather annoying to a user who logged out from a specific view (and perhaps saved it to their favorites) and then wants to go back to it later. By default it will take them to the Login page (because of the `Authorize` attribute) and the expected behavior is that pressing 'log in' would them take them to the page they wanted to go to. –  Jul 24 '15 at 12:33
  • Have you even bothered to do any research or try to solve the problem yourself? Have you tried basic MVC tutorials? – Ben Mar 10 '18 at 12:20

3 Answers3

14

You do not want to make Login as home page. It is not a good design. Mainly because after a user login and enters https://yoursite.com in browser, you do not want to display Login page again.

Instead, you just need to apply [Authorize] to 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 access your home page, s/he will be redirected to Login Page first with ReturnUrl in QueryString.

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

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

  <system.web>
    ...
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880"/>
    </authentication>
  </system.web>
Win
  • 61,100
  • 13
  • 102
  • 181
3

You have two options: 1. Register a default route to your login page

public static void RegisterRoutes(RouteCollection routes)
    {

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

    }
  1. Make Home/Index requiring Authorized access, this way you will make sure that if logged in user is accessing your site he goes straight to authenticated page than login
VidasV
  • 4,335
  • 1
  • 28
  • 50
0

Routing is the best option for it.You can set your default page by make changes in your config file.You will find there are two config files : 1.app.config 2.route.config

By using Route Config you can rewrite your url as well :-

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
            }
        );
    }
}

Note that any MVC application must have at least one route definition in order to function. In the above, a route template named "Default" is added to the routes collection. The items in curly braces enclose Route Parameters, and are represented by the parameter name as a placeholder between the curly braces. Route Segments are separated by forward slashes (much like a standard URL). Notice how the implied relative URL our route specifies matches the MVC convention:

~/{controller}/{action}

You can chane the url as well in the following way:- routes.MapRoute( name: "SiteMap", url: "sitemap", defaults: new { controller = "Static", action = "SiteMap", id = UrlParameter.Optional }, namespaces: new string[] { "name.Web.Controllers" } );

For setting a default page:- the best way is to change your 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 that to be any route you wish.

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

Hope you got a clear idea about it

Shian JA
  • 848
  • 4
  • 15
  • 52