Will "the Mighty" Strohl

HOW TO: Programatically Create A New User in DotNetNuke

I have come to realize that many people have the need in their DotNetNuke (DNN) applications to create a new user "on-the-fly".  Here is how to do it...

First of all, thank you to those who helped me troubleshoot this in the DotNetNuke forums.

You obviously will want to create your own class and take advantage of the Membership, Profile, and Role providers that are in place.  In order to effectively debug this code, I found it necessary to create duplicates of those providers.  Here is the finalized code snippet that I know to work in DNN v4.05.01 through v4.05.03:

' Put the following method into your own class
' This method assumes that you have these local properties:
' "User" As DotNetNuke.Entities.Users.UserInfo
' "PortalSettings" As DotNetNuke.Entities.Portals.PortalSettings
' IMPORTANT - There are extra line breaks added using the VB _
' to prevent breaking the design.
Public Function CreateNewUser(ByVal strUsername As String, _
     ByVal strFirstName As String, ByVal strLastName As String, _
     ByVal strEmailAddress As String, ByVal blnMembershipApproved _
     As Boolean) As DotNetNuke.Security.Membership.UserCreateStatus
     ' preliminary check to be sure that the fields are filled in
     ' (cannot have empty values)
     If String.IsNullOrEmpty(strUsername) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The USERNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strFirstName) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The FIRSTNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strLastName) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The LASTNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strEmailAddress) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The EMAILADDRESS ", _
               "variable for CreateNewUser() cannot be empty."))
     ' validate the e-mail address format
     ' I have a Regular Expression that is used globally for all e-mail
     ' validation... You can use your own here
     If Not _
     MYNAMESPACE.Common.Utilities.RegExLibrary.ValidateEmailFormat( _
          strEmailAddress _
          ) Then _
          Throw New Exception( _
          "The e-mail address was in an invalid format")
     ' create a new user instance and populate it
     Me.User = New DotNetNuke.Entities.Users.UserInfo
     With Me.User
          .AffiliateID = -1
          .DisplayName = String.Concat(strFirstName, " ", _
          .Email = strEmailAddress
          .FirstName = strFirstName
          .IsSuperUser = False
          .Membership.Approved = blnMembershipApproved
          .Membership.CreatedDate = DateTime.Now
          .Membership.Email = strEmailAddress
          ' Use the built-in password generation, or your own
          ' Also, you can ammend this method to pass the password
          ' along with the other info
          '.Membership.Password = _
          '    DotNetNuke.Entities.Users.UserController.GeneratePassword
          .Membership.Password = _
               MYNAMESPACE.Security.PasswordGenerator.Generate(8, 10)
          ' Since the password is generated, force the visitor to
          ' change it when they log in
          .Membership.UpdatePassword = True
          .Membership.Username = strUsername
          .PortalID = Me.PortalSettings.PortalId
          .Username = strUsername
          .Profile.FirstName = strFirstName
          .Profile.LastName = strLastName
          .Profile.PreferredLocale = Me.PortalSettings.DefaultLanguage
          .Profile.TimeZone = Me.PortalSettings.TimeZoneOffset
     End With
     ' I am now handling this in the customized 
     ' profile/membership provider(s)...
     ' It is up to you as to how you want to handle this...
     ' but below should be your "default"
     ' Set the Approved status based on the Portal Settings
     'If Me.PortalSettings.UserRegistration = _
     '    Globals.PortalRegistrationType.PublicRegistration Then
     '    Me.User.Membership.Approved = True
     '    Me.User.Membership.Approved = False
     'End If
     ' This is the line that actually performs the creation of the user
     ' (through the providers)
     ' attempt to create the DNN user
     Dim objStatus As UserCreateStatus = _
     ' set-up the arguments for the status of the user creation
     Dim args As Modules.UserUserControlBase.UserCreatedEventArgs
     If objStatus = UserCreateStatus.Success Then
          args = New _
          args.Notify = True
     Else    ' registration error
          args = New _
          args.Notify = False
     End If
     args.CreateStatus = objStatus
     ' I copied the DNN source code for notifying the user that the 
     ' user account was created, and it is referenced here...
     ' perform some DNN logic
     ' return the enumerated result to be handled accordingly
     Return objStatus
End Function 

blog comments powered by Disqus