The below code is something I use in several apps. Note that some of it is driven by business rules - like a recursive function to make sure that username isn't used already - we create usernames automatically, thus the code. Also, passwords that comes out of the Core password generator seems too obscure for a normal person, so we wrote our own - thus the call to WebHelper.RandomPassword.Generate method. If you need more explanation to the following code, I'll be happy to provide it.
private void btnSave__Click(object sender, System.EventArgs e)
{
DotNetNuke.Entities.Users.UserInfo user_ = new DotNetNuke.Entities.Users.UserInfo();
user_.PortalID = PortalId;
user_.FirstName = txtFName_.Text.Substring(0, 1).ToUpper() + txtFName_.Text.Substring(1);
user_.Profile.FirstName = user_.FirstName;
user_.LastName = txtLName_.Text.Substring(0, 1).ToUpper() + txtLName_.Text.Substring(1);
user_.Profile.LastName = user_.LastName;
try
{
user_.UserID = Session["userId"] != null ? user_.UserID = int.Parse(Session["userId"].ToString()) : -1;
if (user_.UserID > -1)
user_ = new DotNetNuke.Entities.Users.UserController().GetUser(PortalId, user_.UserID);
}
catch { }
user_.Profile.Street = ctlAddress_.Street;
user_.Profile.Unit = ctlAddress_.Unit;
user_.Profile.City = ctlAddress_.City;
user_.Profile.Region = ctlAddress_.Region;
user_.Profile.PostalCode = ctlAddress_.Postal;
user_.Profile.Telephone = ctlAddress_.Telephone;
user_.Email = txtEmail_.Text;
user_.Membership.Email = txtEmail_.Text;
if (user_.UserID > 0)
{
DotNetNuke.Entities.Users.UserController.UpdateUser(PortalId, user_);
}
else
{
user_.Username = user_.Profile.LastName.ToLower() + user_.Profile.FirstName.Substring(0, 1).ToLower();
user_.Membership.Username = user_.Username;
user_.Membership.Password = WebHelper.RandomPassword.Generate(8, 10);
user_.Membership.Approved = false;
user_.AffiliateID = -1;
CreateDNNUser(ref user_);
}
}
private DotNetNuke.Entities.Users.UserInfo CreateDNNUser(ref DotNetNuke.Entities.Users.UserInfo user_)
{
UserCreateStatus createStatus = DotNetNuke.Entities.Users.UserController.CreateUser(ref user_);
if (createStatus == UserCreateStatus.DuplicateUserName || createStatus == UserCreateStatus.UsernameAlreadyExists)
{
user_.Username = user_.Profile.LastName.ToLower() + user_.Profile.FirstName.Substring(0, 1).ToLower() + counter_.ToString();
counter_++;
user_.Membership.Username = user_.Username;
CreateDNNUser(ref user_);
}
return user_;
}