Part 2- How to implement custom Forms Authentication in ASP.NET MVC4 application

In this post, I am going to implement custom Membership Provider in Forms authentication in ASP.NET MVC4 application.

In the previous part of this series, we have implemented very simple and easy forms authentication. Now we will see how we can implement our custom Membership Provider.

In ASP.NET default membership provider API The information in the user and role table was predefined and it cannot be customized. It is very complicated to take full control of the database using ASP.NET default membership provider. So, here we will see how to implement our custom membership provider to take full control of the database and forms authentication mechanism (like validate user, create user, update user, delete user, change password and more).

I have split the entire application split into following parts for making things more simple and understandable.

Login

[N:B: For test you can get username & password from here

X

User List

Full NameEmail IDUsernamePasswordRoles
Sourav Mondal[email protected]Sourav123456User,Admin
Rohan Roy[email protected]Rohan147852User
Jack Richard[email protected]Jack36985User
Brooke Smith[email protected]Brooke489834User
Byron Williams[email protected]Byron668587User
Dustin Jones[email protected]Dustin847340User
Easton Moore[email protected]Easton102609User
Perrin Taylor[email protected]Perrin120484User
Armon Jackson[email protected]Armon138360User
Novak Harris[email protected]Novak156235User
Kistna Martin[email protected]Kistna174110User
Mahari Thompson[email protected]Mahari191986User
Part 2- How to implement custom Forms Authentication in ASP.NET MVC4 application
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <style>
        .menu {
            list-style:none;
            float:right;
        }
         .menu li {
            float:left;

        }
            .menu li a {
                padding:5px;
                margin-right:2px;
                background-color:#f3f3f3;
            }
    </style>
</head>
<body>
    <ul class="menu">
        <li>@Html.ActionLink("Home","Index","Home")</li>
        <li>@Html.ActionLink("My Profile","MyProfile","Home")</li>
        <li>
            @{
                if (Request.IsAuthenticated)
                {
                    using (Html.BeginForm("Logout","MyAccount", FormMethod.Post,new{ id = "logoutForm"}))
                    {
                        <a href="javascript:document.getElementById('logoutForm').submit()">Logout</a>
                    }
                }
                else
                {
                    @Html.ActionLink("Login","Login","MyAccount")
                }
            }
        </li>
    </ul>
    <div style="height:1px;clear:both"></div>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

Posted By :