Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 802
TheoWallcot12 Avatar asked Jul 24 '15 11:07

TheoWallcot12


People also ask

Which is the StartUp page in MVC application?

Right-click your Project within the Solution Explorer. Choose Properties. Select the Web tab on the left-hand side. Under the Start Action section, define the Specific Page you would like to default to when the application is launched.

How do I change the default login page?

u can do this simply by right clicking on ur singin. aspx page and set it as ur start page. The default sign-in page is done in the config file authentication/authorisation sections.


1 Answers

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>
like image 99
Win Avatar answered Nov 15 '22 14:11

Win