Portal Login: Authenticate users against another portal  (C#)

DotNetNuke allows you to create child portals which operate as separate websites. In some cases one might find it desirable to authenticate the users of second portal against the user database of the first portal.

The following module (requires DotNetNuke 4.8 or higher) will do that:

PortalLogin_01.00.00_Install.zip

Barry Sweeney has made a version that uses the EffectiveDate and ExpiryDate from the authentication portal in the target portal: PortalLogin_BarrySweeney.zip

Using The Module

To use the module, create a second portal and in that second portal, create a page and call it Login.

In the Site Settings for the portal, set the page as the Login page.

Install the PortalLogin module and place an instance of it on the Login page you created. Go into the Settings for the module and set the options:

 

Users will be authenticated against the specified portal and their account will be created in the current portal if one does not already exist. The roles for the user from the master portal will also be copied to the second portal.

The Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
using DotNetNuke;
using DotNetNuke.Security;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Membership;
using System.Text;
using System.Data.SqlClient;
using DotNetNuke.Data;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Authentication;
using DotNetNuke.Entities.Portals;
 
namespace PortalLogin
{
    public partial class View : PortalModuleBase
    {
        #region intAuthendicationPortal
        private int intAuthendicationPortal
        {
            get
            {
                int intAuthendicationPortal = 0;
                if (Settings.Contains("AuthendicationPortal"))
                {
                    int.TryParse((string)Settings["AuthendicationPortal"], out intAuthendicationPortal);
                }
                return intAuthendicationPortal;
            }
        }
        #endregion
 
        #region strPortalName
        private string strPortalName
        {
            get
            {
                string strPortalName = "Master Portal";
                if (Settings.Contains("PortalName"))
                {
                    strPortalName = (string)Settings["PortalName"];
                }
                return strPortalName;
            }
        }
        #endregion
 
        #region strPortalURL
        private string strPortalURL
        {
            get
            {
                string strPortalURL = "http://localhost";
                if (Settings.Contains("PortalURL"))
                {
                    strPortalURL = (string)Settings["PortalURL"];
                }
                return strPortalURL;
            }
        }
        #endregion
 
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    // Determine if the Login or Logout view should be displayed
                    mvPortalLogin.SetActiveView((this.UserInfo.UserID == -1) ? vwLogin : vwLogout);
                }
            }
 
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Try to authendicate the user
            UserLoginStatus loginStatus = new UserLoginStatus();
            UserInfo objAuthendicationPortalUserInfo = UserController.ValidateUser(intAuthendicationPortal, 
             txtUserName.Text, txtPassword.Text, "", "", GetIPAddress(), ref loginStatus);
 
            if (!(objAuthendicationPortalUserInfo == null))
            {
                // Check if the password is expired
                UserValidStatus validStatus = UserController.ValidateUser(objAuthendicationPortalUserInfo, 
                 intAuthendicationPortal, false);
                if (validStatus.ToString().Contains("PASSWORD"))
                {
                    ShowPasswordExpired();
                    return;
                }
 
                UserInfo objCurrentPortalUserInfo = UserController.GetUserByName(PortalId, txtUserName.Text);
 
                // If the user does not exist in the current portal add them to it
                if (objCurrentPortalUserInfo == null)
                {
                    AddUserToCurrentPortal(objAuthendicationPortalUserInfo);
                }
 
                // Update the users information
                UpdateUserRoles(objAuthendicationPortalUserInfo);
 
                //Log the user in      
                objCurrentPortalUserInfo = UserController.GetUserByName(PortalId, objAuthendicationPortalUserInfo.Username);
                UserController.UserLogin(PortalId, objCurrentPortalUserInfo, PortalSettings.PortalName, GetIPAddress(), true);
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                lblLoginStatus.Text = "Login Failed, remember that Passwords are case sensitive";
            }
        }
 
        #region AddUserToCurrentPortal
        private void AddUserToCurrentPortal(UserInfo objAuthendicationPortalUserInfo)
        {
            try
            {
                // update the UserPortals table to add the user to the current portal 
                StringBuilder mySqlString = new StringBuilder();
                mySqlString.Append("INSERT INTO {databaseOwner}{objectQualifier}UserPortals ");
                mySqlString.Append("(UserId, PortalId) ");
                mySqlString.Append("VALUES (@UserId, @PortalId) ");
 
                SqlParameter prmUserId = new SqlParameter("@UserId", SqlDbType.Int, 4);
                SqlParameter prmPortalId = new SqlParameter("@PortalId", SqlDbType.Int, 4);
 
                prmUserId.Value = objAuthendicationPortalUserInfo.UserID;
                prmPortalId.Value = PortalId;
                DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmUserId, prmPortalId);
            }
 
            catch (Exception exc)
            {
                Exceptions.LogException(exc);
                lblLoginStatus.Text = exc.Message;
            }
        }
        #endregion
 
        #region UpdateUserInTheCurrentPortal
        private void UpdateUserRoles(UserInfo objAuthendicationPortalUserInfo)
        {
            UserInfo objUserInfo = UserController.GetUserByName(PortalId, objAuthendicationPortalUserInfo.Username);
            RoleController RoleController = new RoleController();
            RoleInfo RoleInfo;
 
            // First remove the user from all roles they are in
            foreach (string strRole in objUserInfo.Roles)
            {
                RoleInfo = RoleController.GetRoleByName(PortalId, strRole);
                if (!(RoleInfo == null))
                {
                    if (RoleController.CanRemoveUserFromRole(PortalSettings, objUserInfo.UserID, RoleInfo.RoleID))
                    {
                        RoleController.DeleteUserRole(PortalId, objUserInfo.UserID, RoleInfo.RoleID);
                    }
                }
            }
 
            // Add the user to the roles that they are in in the Authendication portal           
            foreach (string strRole in objAuthendicationPortalUserInfo.Roles)
            {
                RoleInfo = RoleController.GetRoleByName(PortalId, strRole);
                if (!(RoleInfo == null))
                {
                    RoleController.AddUserRole(PortalId, objUserInfo.UserID, RoleInfo.RoleID, DateTime.MaxValue);
                }
            }
 
        }
        #endregion
 
        #region GetIPAddress
        private string GetIPAddress()
        {
            string sIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (sIPAddress == "")
            {
                sIPAddress = Request.ServerVariables["REMOTE_ADDR"];
            }
            return sIPAddress;
        }
        #endregion
 
        #region Logout
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            try
            {
                PortalSecurity objPortalSecurity = new PortalSecurity();
                objPortalSecurity.SignOut();
 
                Response.Redirect(Globals.NavigateURL());
            }
 
            catch (Exception exc)
            {
                Exceptions.LogException(exc);
            }
        }
        #endregion
 
        #region BackToLogin
        protected void lnkBackToLogin_Click(object sender, EventArgs e)
        {
            // Determine if the Login or Logout view should be displayed
            mvPortalLogin.SetActiveView((this.UserInfo.UserID == -1) ? vwLogin : vwLogout);
        }
        #endregion
 
        #region PasswordReminder
        protected void lnkPasswordReminder_Click(object sender, EventArgs e)
        {
            mvPortalLogin.SetActiveView(vwUpdateAccount);
 
            lnkParentPortal.Text = strPortalName;
            lnkParentPortal.NavigateUrl = strPortalURL;
 
        }
        #endregion
 
        #region ShowPasswordExpired
        private void ShowPasswordExpired()
        {
            mvPortalLogin.SetActiveView(vwExpiredAccount);
 
            lnkParentPorta2.Text = strPortalName;
            lnkParentPorta2.NavigateUrl = strPortalURL;
        } 
        #endregion
    }
}

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNuke® is a registered trademark of DotNetNuke Corporation