Copied to clipboard

Flag this post as spam?

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


  • Jeff 11 posts 73 karma points
    Nov 20, 2012 @ 19:32
    Jeff
    0

    Member Properties & the .Net Membership Provider

    Hi,

    I've followed a number of tutorials on how to use the .NET membership provider to create Members in Umbraco and how to access custom properties. I was able to do that just fine, but I cannot for the life of me figure out how to access default member properties.

    My Issue is that when a new member is created both the name and the login of the Umbraco member are set to the same thing. I would prefer to have the Name as their first and last name, and the login as, well, their login.

    If it helps here is the code I've used to create the member along with custom property access/assignment:

     

    if (Membership.GetUserNameByEmail(email) == null){
                    MembershipUser member = Membership.CreateUser(username, password, email);
                    if (member != null)
                    {
                        Roles.AddUserToRole(member.UserName, "ClassConnect");
                        MemberProfile profile = MemberProfile.GetUserProfile(member.UserName);
                        profile["first_name"] = "John"; //assigns a string to a custom property I've created on the member
                        profile.Save();
                        Response.Redirect("/configure-account.aspx");
                    }
             }

     

     

  • Richard Terris 273 posts 715 karma points
    Nov 21, 2012 @ 10:52
    Richard Terris
    0

    When you created your member type in the back end, did you ensure all these properties were added?

    I have:

    private string AddUser(string Name, string UserName, string Email, string Password, string UserRole)

            {

     

                try

                {

                    MembershipUser member = System.Web.Security.Membership.CreateUser(UserName, Password, Email);

                    System.Web.Security.Roles.AddUserToRole(member.UserName, UserRole);

     

                    // Add the Membership Number to the Member::

                    Member newMember = Member.GetMemberFromLoginName(member.UserName);

                    //var newMember = Membership.GetUser(member.UserName);

     

                    if (newMember != null)

                    {

                        newMember.getProperty("membershipNumber").Value = Convert.ToInt32(txtMemNumber.Value);

                        newMember.getProperty("forename").Value = txtFirstName.Text;

                        newMember.getProperty("surname").Value = txtSurname.Text;

                        newMember.Save();

     

                        Session["RegisterID"] = txtMemNumber.Value;

                    }

     

                    if (Membership.ValidateUser(member.UserName, Password))

                    {

                        String CurrentURL = Request.Url.ToString();

                        FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);

                        //Response.Redirect(CurrentURL, false);

     

                    }

     

                    AddUserSuccess = "Thank you for registering. Enjoy browsing the Member Only Area";

                }

                catch (Exception err)

                {

                    first.Visible = false;

                    second.Visible = true;

                    AddUserSuccess = err.Message;

                }

     

                return AddUserSuccess;

     

     

            }

    but you have to ensure your member type actually has the proprties you're trying to use

  • Jeff 11 posts 73 karma points
    Nov 21, 2012 @ 16:32
    Jeff
    0

    Hi Richard,

    Thanks for the reply, but, I have assigned the properties in the back end; that's not the problem. I'm trying to access the existing properties specifically the Member.Text property which is actually their "Name" in Umbraco. 

    for example if you do:

    MembershipUser member = System.Web.Security.Membership.CreateUser(UserName, Password, Email);
    Lets say for the sake of the example UserName = "doej"
    a member is created in umbraco where their name and login are both "doej"

    I want to change the Name of the member to something else.

    If I were to use the Member API I could have done:

    Member test = Member.MakeNew("John Doe", username, email,MemberType.GetByAlias("ClassConnect"), new umbraco.BusinessLogic.User(0));

    Which allows me to set the "Name" of the member. But, Visual Studios tells me that Member.MakeNew is obsolete and I should use Membership.CreateUser which I've done, but I can't seem to figure out how to access the Name of the Member in Umbraco.

  • Richard Terris 273 posts 715 karma points
    Nov 21, 2012 @ 16:50
    Richard Terris
    0

    If you use Membership.MakeNew does it work?

    Sometimes you can ignore these warnings from VS so long as it builds.

    I'm using .CreateUser above and assigning values to properties from textbox values with:

     if (newMember != null)

                    {

                        newMember.getProperty("membershipNumber").Value = Convert.ToInt32(txtMemNumber.Value);

                        newMember.getProperty("forename").Value = txtFirstName.Text;

                        newMember.getProperty("surname").Value = txtSurname.Text;

                        newMember.Save();

     

                        Session["RegisterID"] = txtMemNumber.Value;

                    }

  • Jeff 11 posts 73 karma points
    Nov 21, 2012 @ 16:55
    Jeff
    0

    That's the thing, I'm trying to avoid using the Member class and instead use the MembershipUser class since it appears that most of the Member class functions are marked as obsolete. I was hoping to do everything using the .Net Membership provider for the sake of learning the new way of doing things. I can't seem to access the default Member properties this way which is posing as a major road block. I'm very tempted to go back to the Member class even though VS keeps warning me of it's obsolete functions.

  • Richard Terris 273 posts 715 karma points
    Nov 21, 2012 @ 17:00
    Richard Terris
    0

    Have you read my code above?

    It's the membershipuser class I'm using

  • Jeff 11 posts 73 karma points
    Nov 21, 2012 @ 17:18
    Jeff
    0

    Yep, I've read it. I'm using it in a similar way too. I can create the new member just fine. It's the access to the built-in member properties that I can't figure out.

    You create your member using the MembershipUser class but later in your code you switch to using the Member class to access your member properties:

    Member newMember = Member.GetMemberFromLoginName(member.UserName);

    That line in visual studios I suspect has a green squiggly line stating it’s obsolete. Since I’m trying to adopt the “new” way of doing things I would prefer to stick with the MembershipUser class and not use both the MembershipUser and Member but I thinking I might have to. As a side note I’ve followed Aaron Powell's tutorial to figure out how to use the .Net Membership provider with Umbraco. I can access and assign values to my “custom” member properties, I just can’t access the “built-in” properties. Here is my code again with a few more comments added.

     

    if (Membership.GetUserNameByEmail(email) == null){
                   //Creates a new Member in Umbraco
                    MembershipUser member = Membership.CreateUser(username, password, email);
                    if (member != null)
                    {
                       //assigns the ClassConnect Member Group to the new member
                        Roles.AddUserToRole(member.UserName, "ClassConnect");
                       //Custom class used to access the custom member properties
                        MemberProfile profile = MemberProfile.GetUserProfile(member.UserName);
                       //assigns a string to a custom property I've created on the member
                       profile["first_name"] = "John";
                       profile.Save();

                        Response.Redirect("/configure-account.aspx");

                    }

             }

  • Richard Terris 273 posts 715 karma points
    Nov 21, 2012 @ 17:22
    Richard Terris
    0

    You need to remember that the Umbraco membership provider is just their own custom .NET membership provider, therefore to add additional properties that aren't in the Umbraco provider you'll have to inherit from the standard .NET provider

    That's the reason I instantiate an instance of the Member class so I can add the additional properties

    If you want to roll your own provider it will have to inherit from the .NET provider and you'll be back to using Member class I think.

    Of course, I'm no expert and if there does turn out to be a better way please share it :)

Please Sign in or register to post replies

Write your reply to:

Draft