Search code examples
asp.netasp.net-membershipforms-authenticationcustom-membershipproviderasp.net-authentication

How to use forms authentication without login control?


How to use forms authentication without login control.I don't want to use asp.net login control in my site.But i have to implement forms authentication and to validate users in my database.


Solution

  • I am assuming that instead of using a login control, you are using a few textboxes(eg username/password) and a logon button. The code could look something like this:

    In your aspx file

    <asp:Textbox runat="server" ID="Username"/>
    <asp:Textbox runat="server" ID="Password"/>
    <asp:Button runat="server" ID="Login" OnClick="Login_OnClick"/>
    
    <asp:Label runat="server" ID="Msg" >
    

    And on server side:

    public void Login_OnClick(object sender, EventArgs args)
    {
    if (Membership.ValidateUser(Username.Text,Password.Text))
    {
         FormsAuthentication.RedirectFromLoginPage(Username.Text,
         NotPublicCheckBox.Checked);
    }
    else
    Msg.Text = "Login failed.";
    }
    

    The forms authentication usually consists of 2 parts:

    • Calling membership API to authenticate/validate the user
    • Using FormsAuthentication API to log the user on (creating auth cookies, etc)

    For more info look at these articles: