Configuring ASP.NET MVC 5 Login Options

        Introduction:


                    ASP.NET MVC 5 added some great new features. One is brand new security feature(Microsoft.AspNet.Identity) which is based on Open Web Interface for .NET(OWIN) middleware. Means we can easily use OWIN security features(Claims and Login with Facebook, Google, Microsoft, Twitter, etc.) in ASP.NET as well as in other OWIN based hosts. The default settings for login are fine but you can easily configure them to meet your needs. In this article, I will tell you how to configure the login options in ASP.NET MVC 5.


        Description:

 

 

                    Create a new ASP.NET MVC 5 application in Visual Studio 2013. Then open  Startup.Auth.cs file in App_Start folder. You will find this line,


        
    app.UseSignInCookies();

                    This line will configure the application and external authentication. Just comment this line and add the following lines of code which has the same effect as the above line,


            //app.UseSignInCookies();  
            app.UseFormsAuthentication(new FormsAuthenticationOptions
            {
                AuthenticationType = FormsAuthenticationDefaults.ApplicationAuthenticationType,
                AuthenticationMode = AuthenticationMode.Active,
                CookieName = FormsAuthenticationDefaults.CookiePrefix + FormsAuthenticationDefaults.ApplicationAuthenticationType,
                LoginPath = FormsAuthenticationDefaults.LoginPath,
                LogoutPath = FormsAuthenticationDefaults.LogoutPath,
            });
            app.SetDefaultSignInAsAuthenticationType(FormsAuthenticationDefaults.ExternalAuthenticationType);
            app.UseFormsAuthentication(new FormsAuthenticationOptions
            {
                AuthenticationType = FormsAuthenticationDefaults.ExternalAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                CookieName = FormsAuthenticationDefaults.CookiePrefix + FormsAuthenticationDefaults.ExternalAuthenticationType,
                ExpireTimeSpan = TimeSpan.FromMinutes(5),
            });


                    In other words the first line of code actually excute the above lines. Now you can easily edit LoginPath, LogoutPath, CookieName, CookieDomain, ReturnUrlParameter and more options using the above lines of code. You can even intercept events during sign in and validate identity. For more options and further detail you can check Understanding OWIN Forms authentication in MVC 5


        Summary:


                    In this article, I showed you how easily you can configure login information including LoginPath, ReturnUrlParameter ,CookieName, etc. in ASP.NET MVC 5 application. Hopefully you will enjoy my this article too.



No Comments