Copied to clipboard

Flag this post as spam?

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


  • andywilde 25 posts 66 karma points
    Jul 01, 2011 @ 18:02
    andywilde
    0

    Error when trying to create member programatically

    when trying to create a member programatically using:

    var m = System.Web.Security.Membership.CreateUser("testuser", "testpassword")

    I get the error: Object reference not set to an instance of an object.

    I am using v4.7 which has been upgraded various times from 3.? through 4 and 4.5.

    I can create members throught the UI.

    I did have to do a fair bit of config file editing to get the search for members facility to work so I'm guseeing I'm missing an entry somewhere. Anyone got any ideas?

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Jul 01, 2011 @ 20:59
    Tim
    0

    You need to use the Umbraco Membership Provider method, rather than the built in Windows Security one. There's an example of creating members programmatically in Umbraco here: http://www.nibble.be/?p=20

    Hope that helps!

    :)

  • andywilde 25 posts 66 karma points
    Jul 05, 2011 @ 18:54
    andywilde
    0

    Tim

    thanks very much for the info, I've got it working now.

    However, there are the following warnings:

    umbraco.cms.businesslogic.member.Member.MakeNew(...) is now depracated

    umbraco.cms.businesslogic.member.Member.AddGroup(int) is obsolete use System.Web.Security.Roles.AddUserToRoles.

    Any thoughts on this?

    Andy

     

  • Javier Barrera 34 posts 85 karma points
    Jul 06, 2011 @ 00:55
    Javier Barrera
    0

    I would like to point out that I got this reply from a CORE member on the topic a while back. I've been using the built membership methods in the .NET library quite successfully, with the exception of having to impliment custom proprties in the Umbraco Membership.

  • Javier Barrera 34 posts 85 karma points
    Jul 06, 2011 @ 00:58
    Javier Barrera
    0

    It could be cleaned up a little, but this has worked well:

        protected bool createUserAccount()
        {
            if (Membership.GetUser(txtEmailAddress.Text) == null)
            {
                MembershipCreateStatus status;

                try
                {
                    MembershipUser user = Membership.CreateUser(txtEmailAddress.Text,
                        txtPassword.Text,
                        txtEmailAddress.Text,
                        System.Configuration.ConfigurationManager.AppSettings["PasswordRecoveryQuestion"],
                        System.Configuration.ConfigurationManager.AppSettings["PasswordRecoveryAnswer"],
                        true,
                        out status);

                    if (status == MembershipCreateStatus.Success)
                    {
                        try
                        {
                            // Add Roles using the static string array DefaultRoles
                            Roles.AddUserToRoles(user.UserName, MemberRoles.DefaultRoles);

                            // Add Umbraco Properties
                            Member member = Member.GetMemberFromEmail(txtEmailAddress.Text);

                            if (member != null || member.Id > 0)
                            {
                                member.Text = txtFirstName.Text + " " + txtLastName.Text;
                                member.getProperty("firstName").Value = txtFirstName.Text;
                                member.getProperty("lastName").Value = txtLastName.Text;
                                member.getProperty("company").Value = txtCompany.Text;
                                member.Save();
                            }

                            return true;
                        }
                        catch (Exception err)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception err)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
  • andywilde 25 posts 66 karma points
    Jul 06, 2011 @ 08:52
    andywilde
    0

    Thanks, I'll give that a try.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 06, 2011 @ 10:08
    Michael Latouche
    0

    Hi,

    I think the problem might rely in your web.config file. Did you specify the "defaultMemberTypeAlias" attribute in the membership provider definition? Or does the one specified exist in your Umbraco backend?

    See this wiki post for more information: our.umbraco.org/.../umbracomembershipprovider-properties

    Hope this helps

    Cheers,

    Michael.

     

  • Arie 4 posts 50 karma points
    Nov 05, 2011 @ 11:33
    Arie
    0

    Thanks for your tip on 'defaultMemberTypeAlias', saved me a lot of time!

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 06, 2011 @ 15:45
    Michael Latouche
    0

    You're welcome :-) !

  • Rob Watkins 369 posts 701 karma points
    Feb 06, 2012 @ 13:09
    Rob Watkins
    0

    How do you create a member with a specified type using the new ASP.NET based API? It seems you can only create one with the defaultMemberTypeAlias looking at that?

    Also, what are the roles? Are they the Member Groups?

Please Sign in or register to post replies

Write your reply to:

Draft