Search code examples
asp.netauthenticationasp.net-membership

How do I use a ASP.NET Login control without using a MembershipProvider?


This is an offshoot of this question.

  • How do I use a Login control if I don't have a MembershipProvider to point it at?
  • Am I understanding the use model correctly?
  • Is it even reasonable to talk about using a Login control without a MembershipProvider?
  • Dose the MembershipProvider do more than just username/password checking?
  • Would it be more reasonable to create my own MembershipProvider with the same authentication logic?

In my case, I don't need a MembershipProvider (I think) as my authentication situation is trivial (one user, one password).

I'm interested partly to "future proof" my page and partly because I'm new and wondering about how stuff works. (I tend to learn about things by running full speed into every corner case I can find :)


Solution

  • You can just drop the asp:Login control in your page, then in the code behind, catch the Login Control's Authenticate event.

    In the Authenticate event, check the username/password that the user has entered. The username/password are properties in the login control. (LoginCtrl.UserName, LoginCtrl.Password)

    If the username/password is correct, just set the event args Authenticated property to True.

    No membership provider is required.

    ex. In the aspx page..

    <asp:Login ID="LoginCtrl" runat="server" DestinationPageUrl="YouAreIn.aspx"></asp:Login>
    

    In Code Behind

    Private Sub Log_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles LoginCtrl.Authenticate
        If LoginCtrl.UserName = "Hello" AndAlso LoginCtrl.Password = "Hello" Then
            e.Authenticated = True
        End If
    

    c#

    void MyLogin_Authenticate(object sender, AuthenticateEventArgs e)
    {
        if(UserName == "Hello" && Password == "Hello")
            e.Authenticated = true;
    }