How to use forms authentication without login control?

Joby Kurian picture Joby Kurian · Apr 11, 2012 · Viewed 11k times · Source

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.

Answer

skub picture skub · Apr 11, 2012

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: