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?
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 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.
@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'?
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
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 :)
"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").
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 :)
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
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
Hmm, no possibility to register new members to Umbraco with Razor?
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
I didn't think razor's intended use was for tasks like this.. it may be a little complex (but I might be wrong!)
@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.
@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
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
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
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;
}
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:
Note I haven't tested this but I think that's how it would work :)
-Tom
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").
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
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
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
Is there a razor example?
Is there a razor example without ASP.NET Wizard Control?
is working on a reply...