Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 30, 2009 @ 09:42
    Jeroen Breuer
    0

    Use Member Or UmbracoMembershipProvider

    Hello,

    I'm writing some code in which I want to create a member. The problem is I don't know what is the best way to create a member. You can use Member.MakeNew or System.Web.Security.Membership.CreateUser. In CreateUser the UmbracoMembershipProvider is called which uses the following code:

    ///

     

    <summary>

     

    /// Adds a new membership user to the data source.

     

    /// </summary>

     

    /// <param name="username">The user name for the new user.</param>

     

    /// <param name="password">The password for the new user.</param>

     

    /// <param name="email">The e-mail address for the new user.</param>

     

    /// <param name="passwordQuestion">The password question for the new user.</param>

     

    /// <param name="passwordAnswer">The password answer for the new user</param>

     

    /// <param name="isApproved">Whether or not the new user is approved to be validated.</param>

     

    /// <param name="providerUserKey">The unique identifier from the membership data source for the user.</param>

     

    /// <param name="status">A <see cref="T:System.Web.Security.MembershipCreateStatus"></see> enumeration value indicating whether the user was created successfully.</param>

     

    /// <returns>

     

    /// A <see cref="T:System.Web.Security.MembershipUser"></see> object populated with the information for the newly created user.

     

    /// </returns>

     

    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion,

     

    string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

    {

     

    if (Member.GetMemberFromLoginName(username) != null)

    status =

    MembershipCreateStatus.DuplicateUserName;

     

    else if (Member.GetMemberFromEmail(email) != null)

    status =

    MembershipCreateStatus.DuplicateEmail;

     

    else

    {

     

    Member m = Member.MakeNew(username, email, MemberType.GetByAlias(m_DefaultMemberTypeAlias), User.GetUser(0));

    m.Password = password;

     

    MembershipUser mUser =

    ConvertToMembershipUser(m);

     

    // custom fields

     

    if (!String.IsNullOrEmpty(m_PasswordRetrievalQuestionPropertyTypeAlias))

    updateMemberProperty(m, m_PasswordRetrievalQuestionPropertyTypeAlias, passwordQuestion);

     

     

    if (!String.IsNullOrEmpty(m_PasswordRetrievalAnswerPropertyTypeAlias))

    updateMemberProperty(m, m_PasswordRetrievalAnswerPropertyTypeAlias, passwordAnswer);

     

     

    if (!String.IsNullOrEmpty(m_ApprovedPropertyTypeAlias))

    updateMemberProperty(m, m_ApprovedPropertyTypeAlias, isApproved);

     

     

    if (!String.IsNullOrEmpty(m_LastLoginPropertyTypeAlias)) {

    mUser.LastActivityDate =

    DateTime.Now;

    updateMemberProperty(m, m_LastLoginPropertyTypeAlias, mUser.LastActivityDate);

    }

     

    // save

    m.Save();

    status =

    MembershipCreateStatus.Success;

     

    return mUser;

    }

     

    return null;

    }

    Now this code still creates an Umbraco Member using Member.MakeNew, but you can't choose your member type for instance. It uses m_DefaultMemberTypeAlias which is set using the following code:

    // test for membertype (if not specified, choose the first member type available)

     

    if (config["defaultMemberTypeAlias"] != null)

    m_DefaultMemberTypeAlias = config[

    "defaultMemberTypeAlias"];

     

    else if (MemberType.GetAll.Length == 1)

    m_DefaultMemberTypeAlias =

    MemberType.GetAll[0].Alias;

     

    else

     

    throw new ProviderException("No default MemberType alias is specified in the web.config string. Please add a 'defaultMemberTypeAlias' to the add element in the provider declaration in web.config");

    Since I'd like to have the option to choose the MemberType I guess using Member.MakeNew is the best option, but on the wiki it says the member class will be obsoleted in Umbraco 4.1. What should I do?

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 30, 2009 @ 09:43
    Jeroen Breuer
    0

    The code was a lot more readable in the editor.... This forum still needs some fixing.....

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 30, 2009 @ 10:05
    Dirk De Grave
    1

    You should go with the membership api's instead of the member api. Create your own membership provider, probably inheriting from the umbraco membership provider implementation and override the CreateUser() method, so you'd have the option to choose whatever member type you'd like instead of the current 'hard-coded' choice.

    Does that sound as an option to you?

     

    Cheers,

    /Dirk

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 30, 2009 @ 10:11
    Jeroen Breuer
    0

    Hey Dirk that sounds like a good solution! Will the UmbracoMembershipProvider be more flexible in Umbraco 4.1? If it's going to be the new standard you should at least have the option to choose your own MemberType.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 30, 2009 @ 10:22
    Dirk De Grave
    0

    I'd suggest that as a new feature on Codeplex. No promises it will make it into the core, but at least it's logged and can be voted for.

     

    Cheers,

    /Dirk

     

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 30, 2009 @ 10:56
    Jeroen Breuer
    0

    I created a workitem on codeplex. You can find it here: http://umbraco.codeplex.com/WorkItem/View.aspx?WorkItemId=25808. It's reported as an issue though.... Couldn't find how to change the type to feature.

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Jan 06, 2010 @ 10:31
    Jeroen Breuer
    0

    I tried to create my own MembershipProvider which inherits from UmbracoMembershipProvider, but it's not as easy as I was hoping. Since almost everything in the UmbracoMembershipProvider is private I can't use most of the code I need. For instance in my own CreateUser method I also need to convert an Umbraco Member to an asp.net MembershipUser. There is a nice method called ConvertToMembershipUser which can do this, but it's private so I can't use it. Also a lot of private strings which I need can't be accessed. This way it's almost impossible to create my own MembershipProvider. Guess for now I'm going to use the old Umbraco Member class until this is fixed.

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Jan 06, 2010 @ 10:36
    Jeroen Breuer
    0

    I also created a workitem for this on codeplex: http://umbraco.codeplex.com/WorkItem/View.aspx?WorkItemId=25855

Please Sign in or register to post replies

Write your reply to:

Draft