Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questions About Custom Membership Provider

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
GB
Hi, i'm trying to implement my own custom membership provider. The problem i have is that my table i am storing my membership information has some additional fields such as the members address.

I found a tutorial on the internet which has the following
Code:
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        MembershipUser user = new MembershipUser(Name, username, providerUserKey, email, passwordQuestion, null, isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
        string sql = "INSERT INTO USERS(USERNAME,PASSWORD,EMAIL,ISACTIVE) VALUES(@UID,@PWD,@EMAIL,@ISACTIVE)";
        db.AddParameter("@UID", username);
        db.AddParameter("@PWD", password);
        db.AddParameter("@EMAIL", email);
        db.AddParameter("@ISACTIVE", (isApproved == true ? "Y" : "N"));
        int i = db.ExecuteNonQuery(sql);

        if (i > 0)
        {
            status = MembershipCreateStatus.Success;
            return user;
        }
        else
        {
            status = MembershipCreateStatus.ProviderError;
            return null;
        }

    }

    public override void UpdateUser(MembershipUser user)
    {
        string sql = "UPDATE USERS SET EMAIL=@EMAIL,ISACTIVE=@ISACTIVE WHERE USERNAME=@UID";
        db.AddParameter("@EMAIL", user.Email);
        db.AddParameter("@ISACTIVE", (user.IsApproved ? "Y" : "N"));
        db.AddParameter("@UID", user.UserName);
        int i = db.ExecuteNonQuery(sql);
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        MembershipUser user = null;
        string sql = "SELECT * FROM USERS WHERE USERNAME=@UID AND ISACTIVE='Y'";
        db.AddParameter("@UID", username);
        SqlDataReader reader = (SqlDataReader)db.ExecuteReader(sql);
        while (reader.Read())
        {
            user = new MembershipUser(Name, reader.GetString(reader.GetOrdinal("username")), null, reader.GetString(reader.GetOrdinal("email")), null, null, (reader.GetString(reader.GetOrdinal("isactive")) == "Y" ? true : false), false, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
        }
        reader.Close();
        return user;
    }

I understand you use the create user wizard control which calls the CreateUser method but how can i get it to include the address details. Would i need to do this manually by setting up my own formview and calling my own method within the class? If this is the case then do i need to do the same for the GetUser and GetAllUsers Methods?

The next thing i can't quite get my head around is that when you create a user or get a user you call MembershipUser method by passing in some values. What does this method do and why does it need to be called when it only accepts a fixed limited number of values?

Appreciate if someone could clear this up for me.

Thanks
 
Hi Ralphiooo,

You can do this 2 ways.

1. Use the membership profile provider and add your extra fields to the properties section of the profile in your web.config file thus...

Code:
<!--Profiles-->
<profile enabled="true" defaultProvider="SqlProfileProvider">
<providers>
<remove name="AspNetSqlProvider"/>
<add name="SqlProfileProvider" connectionStringName="jbData" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, &#xA;Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
<add name="Address" type="string"/>
<add name="AnotherField" type="string"/>
</properties>
</profile>

Then in a CreatedUser event in your CreateUser wizard page you can do this...

Code:
Protected Sub CreatedUser(ByVal sender As Object, ByVal e As EventArgs)

Dim pro As New ProfileCommon()
pro.Initialize(CreateUserWizard1.UserName, True)

pro.Address = (CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address"), TextBox)).Text

pro.Save()

End Sub

The profiles are stored for you in the aspnet_profile table - no need to add any more fields.
You can now access your profiles by simply using

lblAddress.Text = Profile.Address

2. Add more fields to the aspnet_membership table and handle inserts and updates as you normally would in the CreatedUser event.
So, once the user is added, you insert your address values into the membership table.

Get the values back as you normally would when you need them.
Of course this method means you don't get the ease of use of the membership provider.

HTH

J









 
Hi, cheers for your reply i've spent the day reading up on profiles and they seem great. I have a couple of problems i just have to get my head around first. My site has a users area (which will use membership and roles - and hopefully profiles for the extra properties). This is where i update my site. I also have a customers area (including a products area and cart) which i custom build myself. This is where i get abit lost, because i was wondering whether i can have profile elements associated with users area (first name, last name etc) and profiles elements for the customers area (cart).

Appreciate your help once more.

Thanks
 
You will have to create a relationship between your users table and cart table based in the userID which is a GUID column.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top