Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Jan 08, 2012 @ 18:24
    Anthony Candaele
    0

    creating member registration functionality with Razor

    Hi,

    In this excellent blogpost by Mike Taylor on creating a user control for member registration functionality it is explained how to make a user control that enables a user to register on a website. The user is then added to the Member section and a confirmation mail is sent to his mailbox. Only after clicking the confirmation link in the confirmation mail, the Isapproved property of the membertype is set to 'true'.

    This is a great member registration solution. The only thing that bothers me is the usage of a CreateUserWizard control in the user control. I'm started to style the CreateUserWizard and that's when one fully understands why Microsoft came up with ASP.NET MVC . Trying to implement css styles to a web control is really hell.

    So I wonder, if it would be possible to use the functionality to register users as Umbraco members and send them a confirmation mail without using a user control and the CreateUserWizard control, but by the use of Razor scripts?

    If it's doable, I would gladly give it a try.

    Thanks for your advice,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 10, 2012 @ 19:59
    Anthony Candaele
    0

    Hmm, no possibility to register new members to Umbraco with Razor?

  • Mike Taylor 155 posts 353 karma points
    Jan 11, 2012 @ 11:52
    Mike Taylor
    1

    You don't need to use the control - it just makes the tutorial easier. Plus, you can convert the user control to use templates, and then use whatever CSS you want.

    Mike

  • awm 187 posts 376 karma points
    Jan 11, 2012 @ 12:08
    awm
    1

    I didn't think razor's intended use was for tasks like this.. it may be a little complex (but I might be wrong!)

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 11, 2012 @ 12:45
    Anthony Candaele
    0

    @Mike Thanks for the suggestion Mike. I've read that one of the features of asp.net 4.0 was better control over the markup of webcontrols. I thought they had skipped the usercontrolwizard webcontrol :)

    @alimac good to know, it would be pretty handy though if one could do member registration and member profile maniputation from within a Razor script.

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 11, 2012 @ 14:09
    Anthony Candaele
    0

    @Mike I have this other problem, in your tutorial you are creating new members of one membertype 'sitemember'. But I have two membertypes: 'jobseeker' and 'employer'. So how should I change the UmbracoMembershipProvider so a user can register as a 'jobseeker' or 'employer'?

    Thanks for your help,

    Anthony 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 11, 2012 @ 14:15
    Tom Fulton
    1

    Hi Anthony,

    Take a look at this thread, I think you'll need to create a second provider in your web.config and change the defaultMemberTypeAlias - then look into how to change the provider on the CreateUserWizard

    Also see this thread, it seems he changes the MembershipProvider property of the CUW in the CreatingUser event

    -Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 12, 2012 @ 14:34
    Anthony Candaele
    0

    Hi Tom,

    I created the second provider in the web.config file of my web site:

    <providers>

            <clear />

            <add name="UmbracoMembershipProvider" 

                 type="umbraco.providers.members.UmbracoMembershipProvider" 

                 enablePasswordRetrieval="false" 

                 enablePasswordReset="true" 

                 requiresQuestionAndAnswer="false" 

                 defaultMemberTypeAlias="Jobseeker" 

                 umbracoApprovePropertyTypeAlias="isApproved" 

                 umbracoLockPropertyTypeAlias="isLocked" 

                 passwordFormat="Hashed" />

              <add name="UmbracoMembershipProvider2"

                 type="umbraco.providers.members.UmbracoMembershipProvider"

                 enablePasswordRetrieval="false"

                 enablePasswordReset="true"

                 requiresQuestionAndAnswer="false"

                 defaultMemberTypeAlias="Employer"

                 umbracoApprovePropertyTypeAlias="isApproved"

                 umbracoLockPropertyTypeAlias="isLocked"

                 passwordFormat="Hashed" />

            <add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />

    The thing I didn't figure out yet is how to make the createuserwizard control change the membership provider.

    In the tutorial by Mike Taylor only the _CreatedUser event is used, not the _CreateUser event.

    I wonder however if I shouldn't adapt the custom MemberProfile class:

    public class MemberProfile : ProfileBase

        {

            public static MemberProfile GetUserProfile(string username)

            {

                return Create(username) as MemberProfile;

            }

            public static MemberProfile GetUserProfile()

            {

                return Create(Membership.GetUser().UserName) as MemberProfile;

            }

            [SettingsAllowAnonymous(false)]

            public string AuthGuid

            {

                get

                {

                    var o = base.GetPropertyValue("auth_guid");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("auth_guid", value);

                }

            }

            [SettingsAllowAnonymous(false)]

            public string FirstName

            {

                get

                {

                    var o = base.GetPropertyValue("member_firstname");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_firstname", value);

                }           

            }

            [SettingsAllowAnonymous(false)]

            public string LastName

            {

                get

                {

                    var o = base.GetPropertyValue("member_lastname");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_lastname", value);

                }

            }

     

            [SettingsAllowAnonymous(false)]

            public string JobType

            {

                get

                {

                    var o = base.GetPropertyValue("member_jobtype");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_jobtype", value);

                }

            }

     

            [SettingsAllowAnonymous(false)]

            public string JobExperience

            {

                get

                {

                    var o = base.GetPropertyValue("member_jobexperience");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_jobexperience", value);

                }

            }

     

            [SettingsAllowAnonymous(false)]

            public string YearsExperience

            {

                get

                {

                    var o = base.GetPropertyValue("member_years_experience");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_years_experience", value);

                }

            }

            [SettingsAllowAnonymous(false)]

            public string CompanyName

            {

                get

                {

                    var o = base.GetPropertyValue("member_companyname");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_companyname", value);

                }

            }

            [SettingsAllowAnonymous(false)]

            public string Telephone

            {

                get

                {

                    var o = base.GetPropertyValue("member_telephone");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_telephone", value);

                }

     

            }

            [SettingsAllowAnonymous(false)]

            public string Country

            {

                get

                {

                    var o = base.GetPropertyValue("member_country");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("member_country", value);

                }

            }

        }

    As it is this class that is used by the createuserwizard control to get and set values on the member profile.

    Thanks for your help,
    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 12, 2012 @ 14:36
    Anthony Candaele
    0

    Hi Tom,

    I created the second provider in the web.config file of my web site:

    <providers>

            <clear />

            <add name="UmbracoMembershipProvider" 

                 type="umbraco.providers.members.UmbracoMembershipProvider" 

                 enablePasswordRetrieval="false" 

                 enablePasswordReset="true" 

                 requiresQuestionAndAnswer="false" 

                 defaultMemberTypeAlias="Jobseeker" 

                 umbracoApprovePropertyTypeAlias="isApproved" 

                 umbracoLockPropertyTypeAlias="isLocked" 

                 passwordFormat="Hashed" />

              <add name="UmbracoMembershipProvider2"

                 type="umbraco.providers.members.UmbracoMembershipProvider"

                 enablePasswordRetrieval="false"

                 enablePasswordReset="true"

                 requiresQuestionAndAnswer="false"

                 defaultMemberTypeAlias="Employer"

                 umbracoApprovePropertyTypeAlias="isApproved"

                 umbracoLockPropertyTypeAlias="isLocked"

                 passwordFormat="Hashed" />

            <add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />

    The thing I didn't figure out yet is how to make the createuserwizard control change the membership provider.

    In the tutorial by Mike Taylor only the _CreatedUser event is used, not the _CreateUser event.

    I wonder however if I shouldn't adapt the custom MemberProfile class:

    public class MemberProfile : ProfileBase

        {

            public static MemberProfile GetUserProfile(string username)

            {

                return Create(username) as MemberProfile;

            }

            public static MemberProfile GetUserProfile()

            {

                return Create(Membership.GetUser().UserName) as MemberProfile;

            }

            [SettingsAllowAnonymous(false)]

            public string AuthGuid

            {

                get

                {

                    var o = base.GetPropertyValue("auth_guid");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

                    return (string)o;

                }

                set

                {

                    base.SetPropertyValue("auth_guid", value);

                }

            }

            [SettingsAllowAnonymous(false)]

            public string FirstName

            {

                get

                {

                    var o = base.GetPropertyValue("member_firstname");

                    if (o == DBNull.Value)

                    {

                        return string.Empty;

                    }

         

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 12, 2012 @ 14:40
    Tom Fulton
    1

    Hi Anthony,

    The CreatingUser event should fire before the CreatedUser event, so you could handle the member type there by adding that event. 

    You should be able to add it by adding this attribute:  OnCreatingUser="cuw_CreatingUser"

    Then in the codebehind:

            protected void cuw_CreatingUser(object sender, LoginCancelEventArgs e)
            {
                // Add your logic to determine which member type to use, maybe based on a form field?
                // Try setting it like so:
                ((CreateUserWizard)sender).MembershipProvider = "YourMembershipProviderName";
            }

    Note I haven't tested this but I think that's how it would work :)

    -Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 12, 2012 @ 15:08
    Anthony Candaele
    0

    Hi Tom,

    I tried your suggestion:

    protected void cwMember_CreateUserError(object sender, CreateUserErrorEventArgs e)

            {

                // Add your logic to determine which member type to use, maybe based on a form field?

                // Try setting it like so:

                CreateUserWizard cuw = (CreateUserWizard)sender;

                WizardStep cuwStep1 = cuw.FindControl("cuwStep1") as WizardStep;

                DropDownList ddlMemberType = (DropDownList)cuwStep1.FindControl("ddlMemberType");

                if (ddlMemberType.SelectedValue == "employer")

                {

                    cuw.MembershipProvider = "UmbracoMembershipProvider2";

                }

            }

    But this still saves my registered user as a "jobseeker" membertype while it should be saved as a "employer" membertype.

    Reading over part 2 of Mike's tutorial more precisely this sentence: 

    "Make sure the inherits attribute of the provider matches the class name and assembly from your MemberProfile.cs created earlier."

    makes me think I should create a second custom MemberProfile class and then in the CreatedUser event of my createuserwizard control programmatically store and save values in an instance of this second MemberProfile class, based on the selection of the user in the MemberType dropdownlist ("I'm a jobseeker", "I'm an employer").

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 12, 2012 @ 15:11
    Tom Fulton
    1

    Hi Anthony,

    If the member types have different fields, yes, you should probably create a second memberpofile class and reference it on your provider.  Or perhaps you could use the same class but just add the additional fields (not sure which is best)

    But looking at your code, did you reference this from the control via the OnCreatingUser attribute/event?  It looks like you might have useed OnCreateUserError instead of OnCreatingUser, which wouldn't fire unless there was an error :)

    -Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 12, 2012 @ 15:32
    Anthony Candaele
    0

    Hi Tom,

    I feel a little stupid right now, yes, I created the wrong event. Let's say it's the fault of the truncated names in the property editor of VS 2010 :)

    protected void cwMember_CreatingUser(object sender, LoginCancelEventArgs e)

            {

                // Add your logic to determine which member type to use, maybe based on a form field?

                // Try setting it like so:

                CreateUserWizard cuw = (CreateUserWizard)sender;

                WizardStep cuwStep1 = cuw.FindControl("cuwStep1") as WizardStep;

                DropDownList ddlMemberType = (DropDownList)cuwStep1.FindControl("ddlMemberType");

                if (ddlMemberType.SelectedValue == "employer")

                {

                    cuw.MembershipProvider = "UmbracoMembershipProvider2";

                }

            }

    This time I ran the code in the correct event, and it works!

    Indeed, the employer membertype has different properties than the jobseeker membertype, but it works anyway:

    Now I should do some final testing, just to see if I can still register a user as a jobseeker membertype

    Thanks for helping me out, yet again,
    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 12, 2012 @ 15:44
    Anthony Candaele
    1

    yup, registrating a user as a jobseeker membertype still works. So now my member registration form registers users as a jobseeker membertype or an employer membertype , based on their selection from a dropdownlist. The registration functionality is complete, now I can start programming the confirmation email and style the form.

    Thanks again to Tom, Mike and alimac for your help

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Apr 24, 2012 @ 17:32
    Biagio Paruolo
    0

    Is there a razor example?

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Apr 24, 2012 @ 17:33
    Biagio Paruolo
    0

    Is there a razor example without ASP.NET Wizard Control?

Please Sign in or register to post replies

Write your reply to:

Draft