Copied to clipboard

Flag this post as spam?

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


  • Peter Nielsen 159 posts 257 karma points
    Sep 29, 2011 @ 21:49
    Peter Nielsen
    0

    Create a Member programmaticly the right way

    Hi people,

    I have seen arround the umbraco.org that the new Umbraco is using a .NET Membership provider, and it seems that I should create a member the .NET way, and not with the umbraco classes. Is that right?

    RIght now I have this function:

            public static void CreateMember(int memberTypeId, string userName, string userMail, string password) {
                try{
                    if (Member.GetMemberFromEmail(userMail) == null)
                        {
                            MemberType demoMemberType = new MemberType(memberTypeId);
                            Member newMember = Member.MakeNew(userName, demoMemberType, new umbraco.BusinessLogic.User(0));
                            newMember.Email = userMail;
                            newMember.Password = password;
                            newMember.LoginName = userName;
                            //newMember.getProperty(“address”).Value = t;
                            //newMember.getProperty(“city”).Value = txtCity.Text;
                            newMember.Save();
                    }
                    else
                    {
                        //member exists
                    }
                   
                }
                catch(Exception err) {
                    HttpContext.Current.Response.Write("Error: "+ err.Message);
                }
            }

    Taken from the Nibble tutorial: http://www.nibble.be/?p=20

    This seems to work. I can create a Member pretty easy. And maybe I should just be glad and use this. But then I find a page like this:

    http://our.umbraco.org/wiki/how-tos/membership-providers

    wich to me tells me that i should do it another way, but if i try to:

    var member = System.Web.Security.Membership.CreateUser("username", "password"); 

    It doenst seem to work. And i cant understand why defining a variable should create a member.

    Sorry for my stupidity here, but I could use some guidance. Should I just continue down the MakeNew() path, or go the .NET CreateUser() way? And why? :-)

    /Peter

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Sep 30, 2011 @ 07:39
    Markus Johansson
    0

    Interesting question! I tried to use the .Net providers in my latest project but to get generic properties from a member object I had to use the Umbraco Member-class anyway. Seems like using the .NET-providers only would work in very "simple" senarios?

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    Sep 30, 2011 @ 07:42
    Richard Soeteman
    1

    HI Peter,

    I assume you get an error? Check your web.config and make sure defaultMemberTypeAlias is set to the correct alias.

    The variable you create because CreateUser returns the created Member then you can use that object to set email address etc

    Cheers,

    Richard

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Sep 30, 2011 @ 08:06
    Markus Johansson
    0

    I have to add one thing about the .NET membership providers I think that the main reason for the Umbraco-team to implement them is that we can use the standard ASP.NET membership controls like login, loginview, password recovery and so on.

  • Peter Nielsen 159 posts 257 karma points
    Sep 30, 2011 @ 08:46
    Peter Nielsen
    0

    @Enkelmedia Well I think I found s a solution to that problem.

    In the web.config you have to add the custom properties too interact with them:

    <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true">
      <providers>
        <clear />
        <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers" />
      </providers>
      <properties>
        <clear />
        <add name="name" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />
      </properties>
    </profile>

    You can see I added the "name" property as a string :-)

    @Richard > I will try the defaultMemberTypeAlias and see what it do to me :-)

    But is it right... should that one line then create a new user for me? :-)

    var member =System.Web.Security.Membership.CreateUser("username","password");

    /Peter

  • Peter Nielsen 159 posts 257 karma points
    Sep 30, 2011 @ 09:00
    Peter Nielsen
    0

    @Richard > That did the trick. Well certainly can see the smart in this now. It also has a better feel of security when its the .NET classes :-) 

    Now my function looks like this:

            public static void CreateMemberNew(string userName, string userPass, string userMail, string userRole) {    
                try{
                    MembershipUser member = System.Web.Security.Membership.CreateUser(userName, userPass, userMail);
                    System.Web.Security.Roles.AddUserToRole(member.UserName, userRole);
                }
                catch(Exception err) {
                    HttpContext.Current.Response.Write("Error: "+ err.Message);
                }
            }   

    So i can add the group aswell to the user when I create :-)

    But what if I want to add a different MemberTypeAlias for a user? How can i do this then? :-)

    /Peter

     

     

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    Sep 30, 2011 @ 09:18
    Richard Soeteman
    0

    Hi Peter,

    You can only use 1 Member type which is valid since in gthe default asp.net Membership API you also have only one type.

    Cheers,

    Richard

  • Peter Nielsen 159 posts 257 karma points
    Sep 30, 2011 @ 09:22
    Peter Nielsen
    0

    Okay,

    But i can add more in the Umbraco system. Guess that for the ones who uses the old system?! :-)

    Well.. Then I have to stick with the Member Groups instead, even though I cant give them custom properties.

    /Peter

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    Sep 30, 2011 @ 09:37
    Richard Soeteman
    0

    Hi Peter,

    Yes using the legacy system you can use more than 1 member type. If it's to determine the type of user groups might be better. Just add all properties you need on a single member type even the ones you don't need in all situations and you should be good.

    Cheers,

    Richard

  • Peter Nielsen 159 posts 257 karma points
    Sep 30, 2011 @ 12:03
    Peter Nielsen
    0

    Yeah, I think that would be the way im doing it :-)

    Thank you both :-)

    /Peter

Please Sign in or register to post replies

Write your reply to:

Draft