infolink

Saturday, February 27, 2010

How to create your own DotNetNuke Registration page

About a week ago I was working on a project that required user registration. The out of the box registration form included in the core framework of DNN was just not what I wanted. I spent quite a few hours trying to figure out how to create my own. All I could find were modules to purchase or create my own membershipo provider.

Well purchasing a module was not in my scope as I wanted to figure out how. Creating my own provider was just too much work. So I decided to dive into the core code and here is what I came up with:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security.Membership;
using System.Web.Security;
using System.Threading;
using DotNetNuke.Common;
using DotNetNuke.Entities.Users;


namespace DotNetNuke.mymods.Modules.Membership
{
/// -----------------------------------------------------------------------------
///
/// The ViewMembership class displays the content
///

///
///

///
///

/// -----------------------------------------------------------------------------
partial class ViewMembership : PortalModuleBase, IActionable
{

#region Private Members

private string strTemplate;

#endregion

#region Public Methods

public bool DisplayAudit()
{
bool retValue = false;

if ((string)Settings["auditinfo"] == "Y")
{
retValue = true;
}

return retValue;
}

#endregion

#region Event Handlers

/// -----------------------------------------------------------------------------
///
/// Page_Load runs when the control is loaded
///

///
///

///
///

/// -----------------------------------------------------------------------------
protected void Page_Load(System.Object sender, System.EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}

}
public void btnRegister_Click(object sender, EventArgs e)
{
try
{

UserCreateStatus userstatus = UserCreateStatus.AddUser;
UserInfo NewUser = new UserInfo();
NewUser.PortalID = 0;
NewUser.FirstName = txtFirstName.Text;
NewUser.LastName = txtLastName.Text;
NewUser.Username = txtEmail.Text;
NewUser.DisplayName = txtFirstName.Text + " " + txtLastName.Text;
NewUser.Profile.City = txtCity.Text;
NewUser.Profile.Country = "United States";
NewUser.Email = txtEmail.Text;
NewUser.Username = txtEmail.Text;
NewUser.Membership.Password = txtNewPassword.Text;
NewUser.Profile.SetProfileProperty("State", ddlState.SelectedValue);

if (PortalSettings.UserRegistration == Convert.ToInt32(DotNetNuke.Common.Globals.PortalRegistrationType.PublicRegistration))
{
NewUser.Membership.Approved = true;
}
else
{
NewUser.Membership.Approved = false;
}

UserCreateStatus userstatsus = UserController.CreateUser(ref NewUser);
switch (userstatsus.ToString())
{
case "Success":
{
MessageCell.InnerHtml = "you have successfully registered";

break;
}
case "InvalidPassword":
{
MessageCell.InnerHtml = "Your password is too short";
break;
}

default:
MessageCell.InnerText = "" + userstatus.ToString() + "";
break;
}
}


//MessageCell.InnerText = userstatsus.ToString();

catch (Exception ex)
{
Console.Write(ex);
}

}

protected void lstContent_ItemDataBound(System.Object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
string strContent = strTemplate;
string strValue = Null.NullString;

//add content to template
ArrayList objProperties = CBO.GetPropertyInfo(typeof(MembershipInfo));
int intProperty;
foreach (PropertyInfo objPropertyInfo in objProperties)
{
if (strContent.IndexOf("[" + objPropertyInfo.Name.ToUpper() + "]") != -1)
{
strValue = Server.HtmlDecode(DataBinder.Eval(e.Item.DataItem, objPropertyInfo.Name).ToString());
strContent = strContent.Replace("[" + objPropertyInfo.Name.ToUpper() + "]", strValue);
}
}

//assign the content
Label lblContent = (Label)e.Item.FindControl("lblContent");
lblContent.Text = strContent;
}

#endregion

#region Optional Interfaces

/// -----------------------------------------------------------------------------
///
/// Registers the module actions required for interfacing with the portal framework
///

///
///
///
///
///

/// -----------------------------------------------------------------------------
public ModuleActionCollection ModuleActions
{
get
{
ModuleActionCollection Actions = new ModuleActionCollection();
Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false);
return Actions;
}
}

#endregion

}
}

No comments:

Post a Comment

Amazon1