How to login users programmatically in asp.net

Login user programmatically
in .net framework Membership.ValidateUser() method allow us to programmatically verify the supplied username and password are valid. Membership.ValidateUser(username, password) method exists in under System.Web.Security namespace. this method require to pass two parameters.

first parameter name is 'username' and another parameter name is 'password'. both parameters data type is System.String. username parameter value pass the name of the user to be validated and password parameter value pass the password for the specified user. if the username parameter is null or empty then this method throw an HttpException.

this method return a Boolean value. if the supplied user name and password are valid then it return True, otherwise its return false.

in this example code we manually build a login page without Login server control. this login page contains two textboxes and a submit button. two textboxes uses to collect user's user name and password. submit button post the form to web server to verify user's credentials. this asp.net application is configured to use forms authentication and Membership class.

if the login form's supplied user credentials are invalid then login failed and a login failure message display to the user. if the supplied credentials are valid then the user logged in programmatically and user will be redirected to the originally requested url.

FormsAuthentication.RedirectFromLoginPage() method allow us to redirect an authenticated user back to the originally requested url or the default url.

the following asp.net c# example code demonstrate us how can we logged in an user programmatically in an asp.net application.
Login.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        if (Membership.ValidateUser(TextBox1.Text.ToString(), TextBox2.Text.ToString()))
        {
            FormsAuthentication.RedirectFromLoginPage(TextBox1.Text.ToString(), false);
        }
        else {
            Response.Write("Input correct User Name and Password!");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Example: how to login users programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="User Name" AssociatedControlID="TextBox1"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Password" AssociatedControlID="TextBox2"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Default.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Programmatically login example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Are you Logged in?</h1>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br /><br />
        <asp:LoginName ID="LoginName2" runat="server" FormatString="Hi {0}! you are logged in." Font-Size="X-Large" ForeColor="DarkCyan" />
    </div>
    </form>
</body>
</html>
Related asp.net examples