ASP.NET Custom Membership Provider

The following lays out some of the changed made to the web.config for the Membership Provider

Custom Membership Provider

1.Web.config file

We have made some changes in the web.config file.

<system.web>

<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="10">

<providers>

<add name="MyMembershipProvider" type="Providers.FIFAMembershipProvider" connectionStringName="ADConnectionString" adamServer="O=NET" adamFullPath="LDAP://17.8.9.3:" adamPort="3" adamUser="admin" adamPwd="123" adamUserContainer="CN=Users" adamRoleContainer="CN=Roles" />

</providers>

</membership>

</system.web>

You have to specify the defaultProvider name here my default provider name is my custom membership. The name of the default provider is same as my class name. Here type is my assembly name. My authentication thru LDAP server, I am providing information about server, port ,server and password etc.  

Create a class that class inherited from MembershipProvider class.

public class MyMembershipProvider : MembershipProvider

    {

        public MyMembershipProvider()

        {

            //

            // TODO: Add constructor logic here

            //

        }

  }

Overriding the application Name property.

private string _ApplicationName = string.Empty;

 

        public override string ApplicationName

        {

            get

            {

                return _ApplicationName;

            }

            set

            {

                _ApplicationName = value;

            }

        }

Overriding the initilize method,getting the web.config values.

public override void Initialize(string name, NameValueCollection config)

        {

            try

            {

                base.Initialize(name, config);

 

                string ADAMServer = string.Empty;

                string ADAMFullPath = string.Empty;

                string ADAMUserName = string.Empty;

                string ADAMPassword = string.Empty;

                string ADAMPort = string.Empty;

                string ADAMUserContainer = string.Empty;

                string ADAMRoleContainer = string.Empty;

 

                if (!string.IsNullOrEmpty(config["adamServer"]))

                    ADAMServer = config["adamServer"];

             ...


 }

    }

Overriding the create user method

public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

{

      Before creating user check user already exist or not.

If exit send the status back user already exist.

Else

Commit the user.

}

Override the delete user method.

public override bool DeleteUser(string username, bool deleteAllRelatedData)

{

//check if user exists

//remove the record

//save changes

}

Override the method FindUsersByEmail i.e Search user by email

public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)

 {

//set the search filter

//Call the PopulateResults method to pouplate the results

}

Override the findusersbyname method

public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)

{

}

Override the GetAllUsers Method

public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)

 {

}

Overrid the GetNumberOfUsersOnline method

public override int GetNumberOfUsersOnline()

 {

//set the search filter

//create instance of the search result collection

}

Override the getUser Method.

public override MembershipUser GetUser(string username, bool userIsOnline)

 {

}

Override the GetUserNameByemail

public override string GetUserNameByEmail(string email)

 {

}

Override the UpdateUser

public override void UpdateUser(MembershipUser user)

 {

}

Override the ValiateUser,If you are not implemente this method it will give the run time error.

public override bool ValidateUser(string username, string password)

 {

//first, check if the logon exists based on the username and password

//only a valid username and password will be able to obtain the native object

}

Override the ChangePassword method it will return the true or false.

public override bool ChangePassword(string username, string oldPassword, string newPassword)

 {

}

// <summary>

        /// Sets the specific value to the property

        /// for the current logged user

        /// </summary>

        /// <param name="propertyName"></param>

        /// <param name="propetyValue"></param>

        public void SetProperty(string propertyName, string propertyValue)

        {

  }

/// <summary>

        /// gets the specific value of the property

        /// for the current logged user

        /// </summary>

        /// <param name="propertyName"></param>

        /// <param name="propetyValue"></param>

        public string GetProperty(string propertyName)

        {

        }

You can Rename the user name

public bool RenameUserName(string oldUserName, string newUserName)

 {

}

     /// <summary>

        /// Local method to populate the search reults in Membership User Collection

        /// Calling method will catch the exception

        /// </summary>

        /// <param name="srcResults">The result collection</param>

        /// <param name="pageIndex">The Page Index</param>

        /// <param name="pageSize">The Page size</param>

        /// <returns>MembershipUserCollection</returns>

        private MembershipUserCollection PopulateResults(SearchResultCollection srcResults, int pageIndex, int pageSize)

        {

        }

 

/// <summary>

        /// Local method to convert a directory info of an user to MembershipUser

        /// Since there is no object creation, the exception will be caught

        /// and logged in the calling function

        /// </summary>

        /// <param name="UserInfo"></param>

        /// <returns>Returns a user in MembershipUser class</returns>

        private MembershipUser ConvertToMembershipUser(DirectoryEntry UserInfo)

        {
       }

You can enable password reset property

public override bool EnablePasswordReset

        {

            get

            {

                return false;

            }

        }

public override bool EnablePasswordRetrieval

        {

            get

            {

                return false;

            }

        }

public override bool RequiresUniqueEmail

        {

            get

            {

                return false;

            }

        }

You can create new user from presentation layer like this

MembershipUser oNewUser = Membership.CreateUser(txtLoginID.Text, txtPassword.Text, oUserProfile.EmailID);

By Rao B   Popularity  (3922 Views)