Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Apr 26, 2011 @ 19:46
    suzyb
    0

    Membership.CreateUser setting Name property

    I'm using the Membership.CreateUser method to register members with my site.  But I cannot see how to set the "Name" property MemberType's have by default.

    I'm probably missing something easy but I can't think what it is :(

  • suzyb 474 posts 932 karma points
    Apr 28, 2011 @ 14:42
    suzyb
    0

    Does the silence mean it isn't actually possible and I wasn't just missing something after all.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 28, 2011 @ 15:38
    Michael Latouche
    0

    Hi suzyb,

    I'm not sure what you mean, but if your issue is that you cannot pass the MemberType information for creating the user, this must in fact be done in the declaration of your membership provider in your web.config file.

    You can find more info about this at this wiki, page : our.umbraco.org/.../umbracomembershipprovider-properties

    Basically, your web.config definition will look something like this(check the defaultMemberTypeAlias attribute):

    <add name="UmbracoMembershipProvider" 
     
    type="umbraco.providers.members.UmbracoMembershipProvider"
     
    enablePasswordRetrieval="true"
     
    enablePasswordReset="false"
     
    requiresQuestionAndAnswer="true"
     
    defaultMemberTypeAlias="MyDefaultMemberTypeAlias"
     
    passwordFormat="Hashed"
     
    umbracoApprovePropertyTypeAlias="approved"
     
    umbracoLockPropertyTypeAlias="lock"
     
    umbracoFailedPasswordAttemptsPropertyTypeAlias="failed_logins"
     
    umbracoCommentPropertyTypeAlias="comments"
     
    umbracoLastLoginPropertyTypeAlias="last_login"
     
    umbracoPasswordRetrievalQuestionPropertyTypeAlias="question"
     
    umbracoPasswordRetrievalAnswerPropertyTypeAlias="answer" />

    Hope this helps.

    Cheers,

    Michael

  • suzyb 474 posts 932 karma points
    Apr 28, 2011 @ 16:08
    suzyb
    0

    I'm rubbish at explaining things, sorry :/

    When you create a member in the admin, you have to enter name, email, user name and password.  When people register with the site themselves I just want them to enter name, email and password and to use email as the user name.  I've created a user control to do this and use the following snippet of code to create the member.

    MembershipUser member = Membership.CreateUser(Email.Text, Password.Text, Email.Text);
    Roles.AddUserToRole(member.UserName, "Candidates");

    The problem is that sets name as well as user name to the email address where as I only want user name to be the email and name set to whatever the user entered.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 28, 2011 @ 16:30
    Michael Latouche
    0

    Hi suzyb,

    Thanks for the explainations, it is indeed a little more clear like this ;-)

    Well, my best guess is that you will have to use the Umbraco User class to do that. So, after you have created your member, you can get his/her Id (which is of type int) by casting the member.ProviderUserKey.

    Then you can use that Id to load the Umbraco user, and I guess that from there you will be able to change the name back to whatever you wanted.

    using umbraco.cms.businesslogic.member;
    ...


    MembershipUser member = Membership.CreateUser(Email.Text, Password.Text, Email.Text);
    Roles.AddUserToRole(member.UserName, "Candidates");
    Member m = new Member((int)member.ProviderUserKey); // Umbraco Member class
    ...

    Note: I'm not sure you can change the name afterwards (or at least that it will reflect in the members tree), so if changing the name does not work, then I would do the other way around: first create the user with the name he/she entered, and then change the login and set it to the email address.

    Cheers,

    Michael.

  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Apr 29, 2011 @ 06:30
    Richard Soeteman
    0

    Hi Michael,

    You code snippet will work. I only suggest that you check if the member m exists after you created the object and before you assigne the Text Property. Could be that someone canceled the member create process, via events and then you will get weird errors.

    Cheers,

    Richard

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Apr 29, 2011 @ 09:43
    Michael Latouche
    0

    Hi Richard,

    Thanks, you are right, it is indeed best to test that m gets assigned to something prior to doing any operation on it.

    Cheers,

    Michael.

  • suzyb 474 posts 932 karma points
    May 02, 2011 @ 12:28
    suzyb
    0

    Thanks Michael that seems to have worked.  I create the member with the user name set to the name then change it afterwards.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    May 02, 2011 @ 13:59
    Michael Latouche
    0

    Glad you got it sorted out :-)

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft