DotNetNuke: Single Log On

The Single Log On Module only allows one user at a time to log into a DotNetNuke account. If it detects that more than one user has logged in using an account, it logs the previous user out.

Download:  SingleLogOn_01.00.00_Install.zip (this link contains the install and source) (note: If using DotNetNuke 4, You must download, install, place on a page in your DotNetNuke website, and run LinqPrep first. You can obtain it here: http://www.adefwebserver.com/DotNetNukeHELP/Misc/LinqPrep/ )

The Module

After installing the module...

Select Settings

Under Page Settings, uncheck Display Container? and Allow Print?

When a user is logged on they are given a cookie with a random number. This random number is also stored in their profile. If a user logs in again they are given a new cookie with a new random number. When the original user refreshes the page, the cookie will not match and they will be logged out. 

The Code

The following code is used:

using System;
using System.Linq;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Users;
using DotNetNuke.Common;
using DotNetNuke.Security;
using System.Web;

namespace SingleLogOn
{
    public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        int RandomNumberSettings = -1;
        int intRandomNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // Only run if logged in
                if (UserId > -1)
                {                
                    HttpCookie SingleLogOnCookie;
                    SingleLogOnCookie = Request.Cookies["SingleSignOn"];
 
                    // Only try to detect a multiple login if the cookie actually exists
                    if (CookieExists("SingleSignOn"))
                    {
                        String RandomNumberUserID = SingleLogOnCookie.Value;    
                        int RandomNumber = GetRandomNumberFromCookie(RandomNumberUserID);
                        int CookieUserID = GetUserIDFromCookie(RandomNumberUserID);
 
                        // Only try to detect if the current User is the User for the Cookie
                        if (UserId == CookieUserID)
                        {
                            //Get the number stored     
                            if (!(DotNetNuke.Services.Personalization.Personalization.GetProfile(ModuleId.ToString(),
                                "SingleSignOn") == null))
                            {
                                try
                                {
                                    RandomNumberSettings = 
                                        Convert.ToInt32(DotNetNuke.Services.Personalization.Personalization.GetProfile(
                                        ModuleId.ToString(), "SingleSignOn"));
                                }
                                catch (Exception)
                                {
                                    RandomNumberSettings = -1;
                                }
                            }
 
                            // If the numbers do not match  
                            if (RandomNumber != RandomNumberSettings)
                            {
                                if (CookieExists("SingleSignOn"))
                                {
                                    Response.Cookies["SingleSignOn"].Expires = DateTime.Now.AddDays(-1);
                                }
 
                                // Log the user out
                                PortalSecurity objPortalSecurity = new PortalSecurity();
                                objPortalSecurity.SignOut();
 
                                lblLoggedOut.Text = "Another user has logged in using this account. You have been logged out";
                                return;
                            }
                        }
                        else
                        {
                            // User does not have the cookie - Give them a new cookie
                            SetSingleSignOnCookie();
                        }
                    }
                    else
                    {
                        // User does not have the cookie so give them one
                        SetSingleSignOnCookie();
                    }
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
 
        private void SetSingleSignOnCookie()
        {
            //Store a random value for this user in the settings    
            string strRandomNumber = GetRandomNumber();
 
            // Store it in their personalization
            DotNetNuke.Services.Personalization.Personalization.SetProfile(ModuleId.ToString(),
                "SingleSignOn", strRandomNumber);
 
            // Create the cookie            
            HttpCookie objCookie = new HttpCookie("SingleSignOn");
            objCookie.Value = strRandomNumber + "_" + UserId.ToString();
            objCookie.Expires = DateTime.MaxValue;
 
            // Store the cookie - never expires           
            Response.AppendCookie(objCookie);
        }
 
        #region Utility
        private Boolean CookieExists(string strCheckCookie)
        {
            if ((Request.Cookies[strCheckCookie] == null))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
 
        protected string GetRandomNumber()
        {
            Random RandomClass = new Random();            
            intRandomNumber = RandomClass.Next();
            return Convert.ToString(intRandomNumber + 1);
        }
 
        protected int GetRandomNumberFromCookie(string strCookie)
        {
            int intRandomNumber = 0;
            try
            {
                intRandomNumber = Convert.ToInt32(strCookie.Substring(0, strCookie.IndexOf("_")));
            }
            catch
            {
                intRandomNumber = 0;
            }
 
            return intRandomNumber;
        }
 
        protected int GetUserIDFromCookie(string strCookie)
        {
            int intUserID = 0;
            try
            {
                intUserID = Convert.ToInt32(strCookie.Substring(strCookie.IndexOf("_") + 1,
                    strCookie.Length - (strCookie.IndexOf("_")) - 1));
            }
            catch
            {
                intUserID = 0;
            }
            return intUserID;
        }
        #endregion
    }
}

[Back to: The ADefWebserver DotNetNuke HELP WebSite]

 


Buy DotNetNuke Modules from Snowcovered

 DotNetNuke Powered!DotNetNuke is a registered trademark of DotNetNuke Corporation.