Copied to clipboard

Flag this post as spam?

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


  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 25, 2010 @ 13:19
    Mads Krohn
    0

    Custom membership provider problem

     

    So, it seems to me, that I have to implement my own membership provider when I wan't to have more than one member type working with the UmbracoMembershipProvider. I have tried this, but I'm having some trouble getting all the way through.

    In Umbraco I have created a member type called, lets say, NewMemberType.
    I added a new membership provider in the web.config as so:

    <add name="NewMembershipProvider"
                 type="project.MembershipProviders.NewMembershipProvider"
                 defaultMemberTypeAlias="NewMemberType"
                 enablePasswordRetrieval="false"
                 enablePasswordReset="false"
                 requiresQuestionAndAnswer="false"
                 passwordFormat="Hashed"/>

    I then implemented the new very basic membership provider, as so:

    namespace project.MembershipProviders
    {
        public class NewMembershipProvider : umbraco.providers.members.UmbracoMembershipProvider
        {
            public MembershipUser CreateUser(string username, string password, string email)
            {
                MembershipCreateStatus status;
                return CreateUser(username, password, email, null, null, true, out status);
            }

            public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status)
            {
                return CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved,
    null, out status);
            }

            public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
            {
                return base.CreateUser(username, password, email, passwordQuestion, passwordAnswer,
    isApproved, providerUserKey, out status);
            }
        }
    }

    Now, when I try to create a member using the following code:

    var provider = new project.MembershipProviders.NewMembershipProvider();
    var member = provider.CreateUser("mads.krohn", "tester", "[email protected]");

    I get a NullReferenceException. Following is a part of the callstack:

     

     

    [NullReferenceException: Object reference not set to an instance of an object.]
       umbraco.cms.businesslogic.Content.CreateContent(ContentType ct) +188
       umbraco.cms.businesslogic.member.Member.MakeNew(String Name, String LoginName, String Email, MemberType mbt, User u) +662
       umbraco.cms.businesslogic.member.Member.MakeNew(String Name, String Email, MemberType mbt, User u) +55
       umbraco.providers.members.UmbracoMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status)
       project.MembershipProviders.NewMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status)
       project.MembershipProviders.NewMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, MembershipCreateStatus& status)
       project.MembershipProviders.NewMembershipProvider.CreateUser(String username, String password, String email)

     

     

    Now, using Reflector, I know, that UmbracoMembershipProvider.CreateUser() at some point calls the Member.MakeNew() function with the value of this.m_DefaultMemberTypeAlias as the member type.

    My guess it that the member type isn't found and null is returned in the MemberType.GetByAlias() method, causing the final Content.CreateContent() method to fail.

    So, I know I have a missing link somewhere, I just can't put my head around where. Heck, I might even be on the wrong track but any help would be highly appreciated!!

    Kind regards,
    Mads

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 25, 2010 @ 13:24
    Matt Brailsford
    0

    Hey Mads,

    Have you tried referencing the Membership provider from the providers collection?

    I'm not sure whether instantiated the class yourself if not passing in the config values?

    Matt

    var provider = Membership.Providers["NewMembershipProvider"];
  • Eduardo 106 posts 130 karma points
    Nov 25, 2010 @ 13:25
    Eduardo
    0

    Hi Mads,

    I remind you that a user is not a member.

    User  = BackEnd user.

    Member = FrontEnd user.

    What do you want to do? Create new users o members?

    Sincere regards,
    Eduardo Macho

     

  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 25, 2010 @ 13:38
    Mads Krohn
    0

    Thank you Matt, that did the trick!

    I'm actually casting it as so:

    var provider = (NewMembershipProvider)Membership.Providers["NewMembershipProvider"];

    But I guess you found the missing link, much appreciated!

    Eduardo, I do very much intend on creating members. The CreateUser() method might confuse what my intentions are, but it does actually create members and not users :)

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 25, 2010 @ 13:42
    Matt Brailsford
    0

    Cool,

    I probably wouldn't bother casting it to be honest, unless you have some specific methods you've also defined on it, that way if you ever do want to switch it out, it's just a case of changing web.config, where as right now you'd also have to change the cast and rebuild.

    Just a thought =)

    Matt

  • Mads Krohn 211 posts 504 karma points c-trib
    Nov 25, 2010 @ 13:44
    Mads Krohn
    0

    Point taken! Thanks again! :)

  • Eduardo 106 posts 130 karma points
    Nov 25, 2010 @ 13:47
    Eduardo
    0

    Hi Mads,

    Glad to see the problem is solved :)

    Sincere regards,
    Eduardo Macho

Please Sign in or register to post replies

Write your reply to:

Draft