+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding to Profile during login

Adding to Profile during login

6 posts, 0 answered
  1. Tech
    Tech avatar
    26 posts
    Registered:
    13 Sep 2010
    08 Nov 2010
    Link to this post
    Greetings all,

    I have some questions about extending/adding properties to the currently logged in user in Sitfinity 3.x.  I'm using a custom Membership provider that is working just fine, and have extended the MembershipUser class to includes some custom properties for the logged in user, but the more I've read and seen, it seems like the current user Profile (Httpcontext.Current.Profile) seems to be the better place to do this.

    However, I seem to be having a lot of issues adding properties to this guy.  I've added the appropriate elements to the Web.config, but when I try to set those values in my custom Membership provider, they don't seem to "added" to the person's profile.

    Here is some example code that I'm using, which is part of the GetUserDetails() method in my MembershipProvider:

    new MembershipUser(
        Membership.Provider.Name,
        member_id,
        full_name,
        email,
        "",
        "",
        true,
        false,
        join_date,
        DateTime.Today,
        DateTime.Today.AddDays(-1),
        DateTime.Today.AddDays(-1),
        DateTime.Today.AddDays(-1));
     
    ProfileBase profile = HttpContext.Current.Profile;
    profile.SetPropertyValue("Chapter", "Chapter Name");
     
    profile.Save();

    This is really just testing code, but I think it helps illustrate what I'm trying to do. However, when I try to access the "Chapter" property of the current Profile, the value is always blank (regardless of what I put into it).

    I'm _guessing_ what is happening is that the current Profile doesn't actually exist when this MembershipUser is created (or perhaps somehow isn't getting tied back to that person), but I could be completely wrong.

    Anyway, any help would be awesome...thanks!
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    08 Nov 2010
    Link to this post
    Hi Phillip,

    Thank you for using our services.

    You are correct,it might be the case that the current context does not contain the user's profile. Can you please try calling ProfileBase.Create instead and pass the user name of the user you have just created?

    All the best,
    Radoslav Georgiev
    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. Tech
    Tech avatar
    26 posts
    Registered:
    13 Sep 2010
    08 Nov 2010
    Link to this post
    Radoslav,

    Not much luck there.  I tried calling that method as follows, but I'm still not able to get any of the custom profile information for the current user after logging them in.

    ProfileBase profile = ProfileBase.Create(member_id);
    profile.SetPropertyValue("Chapter", "Chapter Name");
    profile.SetPropertyValue("FirstName", "First Name");
    profile.SetPropertyValue("LastName", "Last Name");
    profile.Save();

    However, if I do just try to get a value for the UserName of the current user, it is showing up correctly, just none of the custom properties.

    I originally had all of this information stored in a custom MembershipUser object, just extending the stock .Net class.  But whenever I have to access this information, I have to call the GetUser() method, which in turn has to hit a couple different databases to retrieve this information, and doing this on every single page doesn't seem terribly efficient. 

    I also considered just storing the MembershipUser object in the session state, but I figured that since most of what I'm reading points me at using a Profile object instead, that I would go that route.  However, I'm still somewhat unclear as to where the Profile object is stored. 

    I would be fine creating a custom object to store all this information in, and then manually store it in the session, etc., but it seems a like the structures should already be there to do this, and I'm just missing how to access them.  Also, it seems problematic to keep checking to see if the session state for this custom object exists in my GetUser method, and then having to re-create it, when all that should really already be handled for me.

    Anyway, I hope this is something you can help me out with...thanks!
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Nov 2010
    Link to this post
    Hello Phillip,

    We use the standard ASP.NET methods

    to get/set profile data. The data is stored inside telerik_UserMetaInfo database table.

    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
  5. Tech
    Tech avatar
    26 posts
    Registered:
    13 Sep 2010
    08 Nov 2010
    Link to this post
    Ivan,

    In my custom membership provider DLL, I've overriddent the GetUser() method to load user information into a custom MembershipUser object, because I needed to extend that object with some additional information.

    However, when I need to access any of that custom information (for instance in a custom user control), I need to call the GetUser() method for the currently authenticated user, which then re-loads everything from my database, etc.  In reading through Microsoft's documentation (and through the Telerik documentation for Sitefinity) that storing all of this custom user information in a ProfileBase object makes much more sense.

    So, I tried changing my custom GetUser() method in my custom MembershipProvider.dll  so that I add some custom properties to the Profile through ProfileBase.SetPropertyValue().  However, when I try to access those custom fields in my custom user control, those properties have no value.

    What I'm trying to figure out is why that information isn't being set, or isn't accessible.  I can set/get it using those methods within my GetUser() method, but somewhere between me adding those properties to the Profile, and accessing them via a user control, that information is lost.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Nov 2010
    Link to this post
    Hi Phillip,

    Here is a sample code and short video that the properties should be properly updated. If this code does not work form you, most probably the problem is somewhere in the implementation of your provider.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProfilePropertyControl.ascx.cs" Inherits="Controls_ProfilePropertyControl" %>
     
     
    UserName
    <asp:TextBox runat="server" ID="UserNameText"></asp:TextBox><br />
     
    Upadte Property With
    <asp:TextBox runat="server" ID="FirstNameValue"></asp:TextBox><br />
     
    Current Value for First Name get/set
    <strong  style="color:Red">
    <asp:Label runat="server" ID="ValueLabel"></asp:Label><br />
    </strong>
     
    <asp:Button  runat="server" ID="SetUserValue" Text="UpdateProfileData" /><br />


    code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Profile;
     
    public partial class Controls_ProfilePropertyControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(UserNameText !=null && !String.IsNullOrEmpty(UserNameText.Text))
            {
                var user =  Profile.GetProfile(UserNameText.Text);
                ValueLabel.Text = user.FirstName;
            }
            this.SetUserValue.Click += new EventHandler(SetUserValue_Click);
        }
     
        void SetUserValue_Click(object sender, EventArgs e)
        {
            if (FirstNameValue != null && !String.IsNullOrEmpty(FirstNameValue.Text))
            {
     
                var usr = ProfileBase.Create(UserNameText.Text);
                usr.SetPropertyValue("FirstName" , FirstNameValue.Text);
                usr.Save();
            }
        }
     
    }


    Kind regards,
    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
Register for webinar
6 posts, 0 answered