How to create custom Login & Registration module in Dotnetnuke

In this tip I am not going to explain how to create new DNN module and its settings. Instead, I will try explaining how to register a new user and login from a custom module interface by using Dotnetnuke class methods.

User Registration Interface:
For building a Custom Registration module we need to have required fields like, firstname, lastname, emailId, username and password in a form also need to add required field validation to these fields.

For registering a new user we need to fill the above information to the UserInfo object present within DotNetNuke.Entities.Users namespace.

//C# Coding for User Registration
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Membership;
 
               //Set required information to the UserInfo object
                UserInfo objUser = new UserInfo();
 
                objUser.PortalID = this.PortalId;
                objUser.IsSuperUser = false;
                objUser.FirstName = txtFirstName.text;
                objUser.LastName = txtLastName.text;
                objUser.DisplayName = txtFirstName.text + " " + txtLastName.text;
                objUser.Email = txtEmail.text;
                objUser.Username = txtUserName.text;
 
                //As Dnn keeping username, password and some other information in aspnet_Membership table, So we need to set membership object with required information.
 
               UserMembership objMembership = new UserMembership();
                objMembership.Approved = true;
                objMembership.CreatedDate = DateTime.Now;
                objMembership.Email = txtEmail.text;
                objMembership.Username = txtUserName.text;
                objMembership.Password = txtPassword.text;
 
               //Assign membership object to the UserInfo Object
                objUser.Membership = objMembership;
 
              //To create user we need to call CreateUser method of UserController class which is taking the ref of UserInfo object as parmameter and returning value of type UserCreateStatus type. To create new user it is calling the AddUser stored procedure of DNN database and inserting record in Users table.
 
               //Register User
         UserCreateStatus result = UserController.CreateUser(ref objUser);
 
               //Check status
                if (result == UserCreateStatus.Success)
                {
                    Response.Write("user registered successfully");
                }

Custom User login Interface:
We need to have Username, Password text field and a Login Button in a Form.

– UserController class having ValidateUser method to validate user:

 UserInfo UserController.ValidateUser(int portalId, string Username,
string Password, string authType, string Verificationcode, string Portalname, string IP, ref UserLoginStatus loginStatus)

 UserLoginStatus object is used to check the status of User before login.

– UserController class also having UserLogin method using which we can login into website through programming from our custom module:

void UserController.UserLogin(int portalId, UserInfo user, string portalName, string IP, bool CreatePersistent)

CreatePersistent is used to remember the password after login

//C# Code  for User Login
  protected void btnSignin_Click(object sender, ImageClickEventArgs e)
        {
      
                UserLoginStatus loginStatus = UserLoginStatus.LOGIN_FAILURE;
 
                 //Validate User and set login information to the UserInfo object
                UserInfo objUserInfo = UserController.ValidateUser(this.PortalId, txtUserName.text,
                txtPassword.text, "DNN", "", PortalSettings.PortalName, this.Request.UserHostAddress, ref loginStatus);
 
               //Check validation then login
                if (loginStatus == UserLoginStatus.LOGIN_SUCCESS || loginStatus == UserLoginStatus.LOGIN_SUPERUSER)
                {
                    bool isPersistent = false;
      
                    //User login  
                    UserController.UserLogin(this.PortalId, objUserInfo, PortalSettings.PortalName, this.Request.UserHostAddress, isPersistent);
                    this.Response.Write("Login success");
 
                }
                else
                {
                    this.Response.Write("Login failure");
                }
        }
150 150 Burnignorance | Where Minds Meet And Sparks Fly!