//This method uses the msterCustomerID (remoteID) and looks up the coresponding UserID value in the database for this user.
    //This is done because looking up user by UserName will not work when the username is updated in AMS (Personify).
    //This method returns the UserId in Ektron based on RemoteID/CustomerID from Personify.

    private string AuthenticateCustomer(string customerIdentifier, string email, string userName)
    {
        string sMasterCustomerId = "";
        string pfirstname        = "";
        string plastname         = "";
        int    subCustomerId     = 0;
        string groupslist        = "";

        try
        {
            if (customerIdentifier.Contains('|'))
            {
                var aIdentifiers = customerIdentifier.Split('|');
                sMasterCustomerId = aIdentifiers[0];
                subCustomerId     = int.Parse(aIdentifiers[1]);
            }
            else
            {
                sMasterCustomerId = customerIdentifier;
            }

            //*******Start Custom Dataservice code to get Firstname, Lastname***********//
            Uri ServiceUri = new Uri(svcUri_Base);
            LoginUsertokentico.WriteError("AuthenticateCustomer ID ==>", sMasterCustomerId.ToString());
            PersonifyEntitiesBase DataAccessLayer = new PersonifyEntitiesBase(ServiceUri);
            DataAccessLayer.IgnoreMissingProperties = true;
            DataAccessLayer.Credentials             = new NetworkCredential(svcLogin, svcPassword);

            var userdetails =
                DataAccessLayer.CusNameDemographics.Where(p => p.MasterCustomerId == sMasterCustomerId)
                .Select(o => o)
                .ToList()
                .FirstOrDefault();

            pfirstname = null;
            plastname  = null;
            if (userdetails == null)
            {
                pfirstname = @" ";
                plastname  = @" ";
            }
            if (userdetails != null && string.IsNullOrWhiteSpace(userdetails.FirstName))
            {
                pfirstname = @" ";
            }
            else
            {
                if (userdetails != null)
                {
                    pfirstname = userdetails.FirstName;
                }
            }
            if (userdetails != null && string.IsNullOrWhiteSpace(userdetails.LastName))
            {
                plastname = @" ";
            }
            else
            {
                if (userdetails != null)
                {
                    plastname = userdetails.LastName;
                }
            }
            //*******End Custom Dataservice code to get Firstname, Lastname***********//

            string[] memberGroups = GetImsroles(sMasterCustomerId, subCustomerId);

            if (memberGroups.Length > 0)
            {
                foreach (string s in memberGroups)
                {
                    if (s.Length > 0)
                    {
                        groupslist += s + ",";
                    }
                }
            }

            groupslist += "peronifyUser" + ",";



            string login = objKenticoService.CreateUpdateLoginUserinKentico(
                userName,
                pfirstname,
                plastname,
                email,
                groupslist,
                true,
                false);
            userinfo uInfo = new userinfo
            {
                ID         = sMasterCustomerId,
                Token      = Session["PersonifyToken"].ToString(),
                email      = email,
                firstname  = pfirstname,
                lastname   = plastname,
                username   = userName,
                groupNames = groupslist
            };


            Session["userClass"] = uInfo;
            return(login);
        }
        catch (Exception exception)
        {
            EventLogProvider.LogException("AuthenticateCustomer", "Get", exception);
            LoginUsertokentico.WriteError("AuthenticateCustomer", exception.ToString());
            return(null);
        }
    }
    //This method uses the msterCustomerID (remoteID) and looks up the coresponding UserID value in the database for this user.
    //This is done because looking up user by UserName will not work when the username is updated in AMS (Personify).
    //This method returns the UserId in Ektron based on RemoteID/CustomerID from Personify.

    private string AuthenticateCustomer(string customerIdentifier, string email, string userName)
    {
        string sMasterCustomerId = "";
        string pfirstname        = "";
        string plastname         = "";
        int    subCustomerId     = 0;
        string groupslist        = "";

        try
        {
            var aIdentifiers = customerIdentifier.Split('|');
            sMasterCustomerId = aIdentifiers[0];
            subCustomerId     = int.Parse(aIdentifiers[1]);

            //*******Custom Dataservice code to get Firstname, Lastname*****//
            var personifyuser = new Personify.WebControls.Base.Business.PersonifyIdentity
            {
                ContainerName    = "Kentico",
                CurrencyCode     = "USD",
                MasterCustomerId =
                    sMasterCustomerId,
                SubCustomerId =
                    subCustomerId
            };
            //  var userdetails = new DemographicProvider().GetCusNameDemographic(personifyuser);
            // Uri ServiceUri = new Uri("http://smemi.personifycloud.com/PersonifyDataServices/PersonifyDatasme.svc");
            Uri ServiceUri = new Uri(svcUri_Base);

            LoginUsertokentico.WriteError("AuthenticateCustomer ID ==>", sMasterCustomerId.ToString());

            PersonifyEntitiesBase DataAccessLayer = new PersonifyEntitiesBase(ServiceUri);
            //  DataAccessLayer.Credentials = new NetworkCredential("admin", "admin123");
            DataAccessLayer.Credentials = new NetworkCredential(svcLogin, svcPassword);
            // var userdetails = DataAccessLayer.CusNameDemographics.Where(p => p.MasterCustomerId == sMasterCustomerId).Select(o => o).ToList().FirstOrDefault();
            var userdetails =
                DataAccessLayer.CusNameDemographics.Where(p => p.MasterCustomerId == sMasterCustomerId)
                .Select(o => o)
                .ToList()
                .FirstOrDefault();
            // var userdetails = new DemographicProvider().GetCusNameDemographic(personifyuser);
            pfirstname = null;
            plastname  = null;

            if (userdetails == null)
            {
                pfirstname = @" ";
                plastname  = @" ";
            }
            if (userdetails != null && string.IsNullOrWhiteSpace(userdetails.FirstName))
            {
                pfirstname = @" ";
            }
            else
            {
                if (userdetails != null)
                {
                    pfirstname = userdetails.FirstName;
                }
            }
            if (userdetails != null && string.IsNullOrWhiteSpace(userdetails.LastName))
            {
                plastname = @" ";
            }
            else
            {
                if (userdetails != null)
                {
                    plastname = userdetails.LastName;
                }
            }
            //*******End Custom Dataservice code to get Firstname, Lastname***********//

            string[] memberGroups = GetImsroles(sMasterCustomerId, subCustomerId);

            if (memberGroups.Length > 0)
            {
                foreach (string s in memberGroups)
                {
                    if (s.Length > 0)
                    {
                        groupslist += s + ",";
                    }
                }
            }

            groupslist += "peronifyUser" + ",";



            string login = objKenticoService.CreateUpdateLoginUserinKentico(
                userName,
                pfirstname,
                plastname,
                email,
                groupslist,
                true,
                false);
            userinfo uInfo = new userinfo
            {
                ID         = sMasterCustomerId,
                Token      = Session["PersonifyToken"].ToString(),
                email      = email,
                firstname  = pfirstname,
                lastname   = plastname,
                username   = userName,
                groupNames = groupslist
            };


            Session["userClass"] = uInfo;
            return(login);
        }
        catch (Exception exception)
        {
            EventLogProvider.LogException("AuthenticateCustomer", "Get", exception);
            LoginUsertokentico.WriteError("AuthenticateCustomer", exception.ToString());
            return(null);
            // evp.LogEvent("Source", "EventCode", exception,1);
            //evp.LogEvent("AuthenticateCustomer", "Get", exception.ToString(),"","",0,"",0,"","",0,"","","",DateTime.Now);
        }
    }