More in this section

Forums / Developing with Sitefinity / Loading Custom Profile with AD

Loading Custom Profile with AD

3 posts, 0 answered
  1. Philip Senechal
    Philip Senechal avatar
    69 posts
    Registered:
    07 Apr 2008
    18 May 2010
    Link to this post
    This isn't really a Sitefinity question, but since I'm trying to do this within Sitefinity, I thought this might be a good place to get assistance.

    We've hooked Sitefinity up to Active Directory and because of that, I have created an additional Profile system where I can store additional information. This whole setup works great except for one simple thing. When a new user hits the website for the first time, the Profile system must create an entry in the table that stores the profile information. It does this without any problems other than that the first page the user hits can't seem to pull the new profile information that was just created.

    I have tried putting the code in several places so that the newly created profile data is available, but I can't seem to find a place that actually works. I have tried the Page_PreInit method in the master page and the Page_PreInit method in the code behind of the cmsentrypoint.aspx page. Neither one seems to do it early enough so that the first page has access to the new information.

    Is there a different place I can try putting it, or is there something I can do to refresh the profile information so that the first page a new user hits actually has the information available?

    Much appreciated for the help =)
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 May 2010
    Link to this post
    Hello Philip Senechal,

    You could use HttpModule and create the user data in BeginRequest.

    public class HttpModule : IHttpModule
    {
        public HttpModule()
        {
     
        }
     
        #region IHttpModule Members
     
        public void Dispose()
        {
             
        }
     
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
     
        void context_BeginRequest(object sender, EventArgs e)
        {
            // do your logic here
            HttpContext context = HttpContext.Current;
          
        }
     
        #endregion
    }


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Philip Senechal
    Philip Senechal avatar
    69 posts
    Registered:
    07 Apr 2008
    19 May 2010
    Link to this post
    I wasn't able to access the Profile object in the HttpModule, so what I ended up doing was just returning the values from the method that writes the values to the table and using Profile.SetPropertyValue to manually set them if they don't exist. Looks something like this...

    public partial class Sitefinity_cmsentrypoint : Telerik.Cms.Web.InternalPage
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /* CHECKS TO SEE IF THERE IS A PROFILE ENTRY IN tUSER AND ADDS OR UPDATES THE ENTRY */
            if (Profile.SessionDate.Date < DateTime.Today)
            {
                if (Profile.SessionDate.Date == Convert.ToDateTime("01/01/0001"))
                {
                    string name = ISProfile.UpdateProfile(Profile.UserName, "INSERT");
                    Profile.SetPropertyValue("Photo", "~/Sitefinity/Common/Images/defaultavatar.gif");
                    Profile.SetPropertyValue("FirstName", name);
                    Profile.Save();
                }
                else
                {
                    string name = ISProfile.UpdateProfile(Profile.UserName, "UPDATE");
                }
            }
        }
    }

    public static string UpdateProfile(string userName, string cmd)
    {
        string name = "";
        if (userName.Contains("DOMAIN\\"))
        {
            userName = userName.Replace("DOMAIN\\", "");
        }
     
        string query = "SELECT tEmployee.EmpID, Login, EmpFNm, EmpLNm, EmpEmail, PhNbr, BossID, tDepartment.DeptID, tDepartment.DeptNm FROM ((tEmployee LEFT JOIN tDepartmentContact on tDepartmentContact.EmpID = tEmployee.EmpID) LEFT JOIN tDepartment on tDepartment.DeptID = tDepartmentContact.DeptID) LEFT JOIN tEmployeePhone on tEmployeePhone.EmpID = tEmployee.EmpID WHERE tEmployee.Login = '" + userName + "' AND tEmployee.ActInd = 'Y' AND tDepartmentContact.PriDeptInd = 'Y' AND tEmployeePhone.PhTypID = 1";
        DataTable dt = DataAccess.SqlGetDataTable(query, "CONNECTIONSTRINGNAME");
        if (dt.Rows.Count > 0)
        {
            if (cmd == "INSERT")
            {
                query = "INSERT INTO tUser (EmpID, Login, EmpFNm, EmpLNm, EmpEmail, PhNbr, BossID, DeptID, DeptNm, SessionDt, Photo) VALUES(" + dt.Rows[0]["EmpID"] + ", '" + dt.Rows[0]["Login"] + "', '" + dt.Rows[0]["EmpFnm"] + "', '" + dt.Rows[0]["EmpLNm"] + "', '" + dt.Rows[0]["EmpEmail"] + "', '" + dt.Rows[0]["PhNbr"] + "', " + dt.Rows[0]["BossID"] + ", " + dt.Rows[0]["DeptID"] + ", '" + dt.Rows[0]["DeptNm"] + "', '" + DateTime.Now.ToString() + "','~/Sitefinity/Common/Images/defaultavatar.gif')";
            }
            else
            {
                query = "UPDATE tUser SET EmpID = " + dt.Rows[0]["EmpID"] + ", Login = '" + dt.Rows[0]["Login"] + "', EmpFNm = '" + dt.Rows[0]["EmpFNm"] + "', EmpLNm = '" + dt.Rows[0]["EmpLNm"] + "', EmpEmail = '" + dt.Rows[0]["EmpEmail"] + "', PhNbr = '" + dt.Rows[0]["PhNbr"] + "', BossID = " + dt.Rows[0]["BossID"] + ", DeptID = " + dt.Rows[0]["DeptID"] + ", DeptNm = '" + dt.Rows[0]["DeptNm"] + "', SessionDt = '" + DateTime.Now.ToString() + "' WHERE Login = '" + userName + "'";
            }
            DataAccess.SqlWriteTableData(query, "SiteFinity");
     
            name = dt.Rows[0]["EmpFNm"] + " " + dt.Rows[0]["EmpLNm"];
        }
     
        return name;
    }

    Not the most elegant solution, but it works =)  Thanks for the help!
Register for webinar
3 posts, 0 answered