Paulo Morgado

.NET Development & Architecture
How to use Forms Authentication's User Definitions with the Login Control

In ASP.NET 1.1 we already had definitions of name and password credentials within the configuration file. We just didn't have a Login control.

Now we have both but the Login control is set by default to use the default membership provider.

How can we use both of them together?

Well, there are two ways, depending on your will or needs:

  1. Using the Login control with FormsAuthentication

    This is the easiest way, you just need to handle the Authenticate event of the Login control.

    Login.aspx

    <!-- ... -->

    <asp:Login ID="Login1" runat="server" DisplayRememberMe="False" OnAuthenticate="Login1_Authenticate">

    </asp:Login>

    <!-- ... -->

     

    Login.aspx.cs

    // ...

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

    {

        e.Authenticated = FormsAuthentication.Authenticate(Login1.UserName, Login1.Password);

    }

    // ...

     

  2. Using FormsAuthentication as the membership provider

    This one is a bit trickier.

    First you don't need to handle the Authenticate event.

     

    Login.aspx

    <!-- ... -->

    <asp:Login ID="Login1" runat="server" DisplayRememberMe="False">

    </asp:Login>

    <!-- ... -->

    Then you need to implement a membership provider that uses FormsAuthentication.

     

     

    FormsAuthenticationMembershipProvider.cs

    public class FormsAuthenticationMembershipProvider : System.Web.Security.MembershipProvider

    {

        private string applicationName;

     

        public override string ApplicationName

        {

            get { return this.applicationName; }

            set { this.applicationName = value; }

        }

     

        public override bool EnablePasswordReset

        {

            get { return false; }

        }

     

        public override bool EnablePasswordRetrieval

        {

            get { return false; }

        }

     

        public override bool ValidateUser(string username, string password)

        {

            return FormsAuthentication.Authenticate(username, password);

        }

     

        // Not Implemented MembershipProvider Members

    }

    And register it in the configuration file.

Published Friday, April 20, 2007 12:38 AM by Paulo Morgado

Comments

# re: How to use Forms Authentication's User Definitions with the Login Control@ Friday, April 20, 2007 10:29 AM

Or set the MembershipProvider property of the login control if you only want to use that provider in the login page.

Luis Abreu

# re: How to use Forms Authentication's User Definitions with the Login Control@ Wednesday, September 26, 2007 1:44 AM

can any tell me a way to override the authenticate method, I know it is a sealed class, but I require it.

satellite

# re: How to use Forms Authentication's User Definitions with the Login Control@ Wednesday, September 26, 2007 4:52 AM

Which authenticate method?

Paulo Morgado

# Improving The Page Flow Application Block: Removing Database Dependencies@ Thursday, October 25, 2007 5:57 PM

Introduction Especially in development and demonstration scenarios, the dependency on a database can

Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required)