Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary Smith 27 posts 47 karma points
    Sep 02, 2010 @ 23:31
    Gary Smith
    0

    member profile problem 4.5.x

    I have a custom registration form using the asp.net membership provider.  I have also created a custom profile provider as well.  After registration, I want to set some settings into the profile for the user (address, etc).

    Here are the steps I took:

    • Created a user typed called "Registered User" (alias "RegisteredUser") under Member Types.
    • Added a generic property of type string called "Address"
    • Tweak the web.config to include the default provider and default member type.
      ...
       <!-- Membership Provider -->
          <membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
            <providers>
              <clear />
              <add name="UmbracoMembershipProvider"
              type="umbraco.providers.members.UmbracoMembershipProvider" 
              enablePasswordRetrieval="true" enablePasswordReset="true" 
              requiresQuestionAndAnswer="false" defaultMemberTypeAlias="RegisteredUser"
              passwordFormat="Clear" />
              <add name="UsersMembershipProvider" 
              type="umbraco.providers.UsersMembershipProvider" 
              enablePasswordRetrieval="false" enablePasswordReset="false" 
              requiresQuestionAndAnswer="false" passwordFormat="Clear" />
            </providers>
          </membership>
      
      ...
          <!-- Profile Provider -->
              <!--<add name="address" allowAnonymous ="false"
              provider="UmbracoMemberProfileProvider" type="System.String" />
              <add name="city" allowAnonymous ="false" 
              provider="UmbracoMemberProfileProvider" type="System.String" />
              <add name="state" allowAnonymous ="false" 
              provider="UmbracoMemberProfileProvider" type="System.String" />
              <add name="zip_code" allowAnonymous ="false" 
              provider="UmbracoMemberProfileProvider" type="System.String" />
              <add name="phone" allowAnonymous ="false" 
              provider="UmbracoMemberProfileProvider" type="System.String" />-->
              </properties>
          </profile>     
      
      
    • Created a profile that inherits from ProfileBase with a public property of Address, accessing underlying base member "Address"
       
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Profile;
      using System.Web.Security;
      
      namespace NPS10x10.Umbraco.Profile
      {
          public class NPSProfile : ProfileBase
          {
      
              public static NPSProfile CurrentUser
              {
                  get
                  {
                      return (NPSProfile)(ProfileBase.Create(Membership.GetUser().UserName));
                  }
              }
      
              public static NPSProfile GetUser(string userName)
              {
                  return (NPSProfile)(ProfileBase.Create(userName));
              }
      
              public string Address
              {
                  get
                  {
                      return ((string)(base["Address"]));
                  }
                  set
                  {
                      base["address"] = value;
                      Save();
                  }
              }
          }
      }
      
    • I let the framekwork create the user.
    • On created user callback (inherited from the registration wizard page), I get the user profile for the new user using default framework code [ (NPSProfile )ProfileBase.Create(userName) ]
      using System;
      using System.Collections;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.Security;
      using umbraco;
      using umbraco.BusinessLogic;
      using umbraco.cms.businesslogic;
      using umbraco.cms.businesslogic.member;
      using umbraco.cms.businesslogic.propertytype;
      using umbraco.interfaces;
      
      namespace NPS10x10.Umbraco
      {
          public partial class NPSRegister : System.Web.UI.UserControl
          {
      
              protected void Page_Load(object sender, EventArgs e)
              {
              }
      
              protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
              {
                  Profile.NPSProfile p = 
                     Profile.NPSProfile.GetUser(
                      (WizardStep1.ContentTemplateContainer.FindControl("UserName") as TextBox).Text);
                  p.Address = (WizardStep1.ContentTemplateContainer.FindControl("textAddress") as TextBox).Text;
                  p.Save();
              }
      
          }
      }
      

    Problem is, value is never saved to the database.  When it loops through the values of my customer profile in the member profile provider (umbraco) it never finds the match.  The m.getProperty(spv.Name) check below always comes back null.  spv.Name does have the "Address" value for name, and the address property does exist for the member type "RegisteredUser" 

    MemberProfileProvider.cs line #149-155:
                foreach (SettingsPropertyValue spv in collection) {
                    if (!authenticated && !(bool)spv.Property.Attributes["AllowAnonymous"])
                        continue;
    
                    if (m.getProperty(spv.Name) != null)
                        m.getProperty(spv.Name).Value = spv.PropertyValue;
                }
    
    

    Is there something I'm missing? Logically, this code should work.

  • Gary Smith 27 posts 47 karma points
    Sep 02, 2010 @ 23:34
    Gary Smith
    0

    one more commet (tried editing but throws xslt error).  I have commented out the Address addition to the profile provider section in the web.config because if I add it an error is thrown saying it already exists (as well as all of the other types I have been trying).  I've tried to do a <clear /> first but that failed.  There are no other instances of this in the web.config

  • Gary Smith 27 posts 47 karma points
    Sep 03, 2010 @ 18:14
    Gary Smith
    0

    I've made some headway on this.  The member type property is names "Address" and the alias is "address".  The .Net code uses "Address" for the property and populates the base value of "address".  It seems that it's a casing issue.  I figured the simplist way to resolve the problem would be to change the alias name to "Address", but it gives me an error when I use "A" instead of "a". 

    Riddle me this: Why?

    Does this mean that I need to ensure all my .Net properties name start with lower case?

  • Chad Rosenthal 272 posts 474 karma points
    Feb 08, 2011 @ 17:19
    Chad Rosenthal
    0

    Did you ever figure this out?

  • Gary Smith 27 posts 47 karma points
    Feb 08, 2011 @ 18:33
    Gary Smith
    0

    Looking at the last code I used, it appears that we created our own profile provider which inherits ProfileBase and use that to set the values of the base using the lower cased properties. We also omitted all of the lines from the web.config description the properties (only a reference to the provider). By doing this, our profile class has more meaningful names (such as FirstName instead of firstName). We also added a couple static helper methods to get the current profile so you can just do CurrentProfile.FirstName

             public string LastName
            {
                get
                {
                    return ((string)(base["lastname"]));
                }
                set
                {
                    base["lastname"] = value;
                    Save();
                }
            }

     

      <!-- Profile Provider -->
      <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true" inherits="NPS10x10.Umbraco.Profile.NPSProfile, NPS10x10.Umbraco.Profile">
       <providers>
        <clear/>
        <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers" />
       </providers>
       <properties>
        <clear/>
        <!--<add name="address" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />
            <add name="city" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />
            <add name="state" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />
            <add name="zip_code" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />
            <add name="phone" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />-->
       </properties>
      </profile>
  • Matt Taylor 873 posts 2086 karma points
    May 10, 2011 @ 15:14
    Matt Taylor
    0

    From above ...

    I've made some headway on this.  The member type property is names "Address" and the alias is "address".  The .Net code uses "Address" for the property and populates the base value of "address".  It seems that it's a casing issue.  I figured the simplist way to resolve the problem would be to change the alias name to "Address", but it gives me an error when I use "A" instead of "a".

    I'm having the same issue here. I've been following http://www.aaron-powell.com/umbraco-members-profiles and couldn't figure out why it wasn't working.
    I have created a profile provider which inherits from ProfileBase and the corresponding member type property only gets written to if the name of the property name in the provider is in lower case.

    The provider code is like so:

    public class MemberProfileTest : ProfileBase
        {
            public static MemberProfileTest GetMemberProfile(string username)
            {
                return Create(username) as MemberProfileTest;
            }

            [SettingsAllowAnonymous(false)]
            public string Age
            {
                get
                {
                    var o = base.GetPropertyValue("age");
                    if (o == DBNull.Value)
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    base.SetPropertyValue("age", value);
                }
            }
        }

    I'm setting the profile values like this:

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
            {
                MemberProfileTest profile = MemberProfileTest.GetMemberProfile(CreateUserWizard1.UserName);
                profile.Age = "99";
                profile.Save();           
            }

    The web.config looks like this:

    <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true" inherits="MembershipTest.MemberProfileTest">
       <providers>
           <clear/>
           <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers"/>
       </providers>
    </profile>

     

    As I say, for some reason it does not work unless I change the case of the provider property to

    public string age

    and set the value like so

    profile.age = "99";

     

    Any idea why this would be?

    Regards,

    Matt

  • Matt Taylor 873 posts 2086 karma points
    May 10, 2011 @ 17:04
    Matt Taylor
    1

    Solved my problem.
    It seems you can do one of two things.

    Either add the properties you want to access to the 'properties' part of profile section in the web.config using the Umbraco alias names and then create properties in your provider with different names.

    or

    Don't bother adding the 'properties' part of profile section in the web.config and just create properties in your provider class, but making sure you name them exactly the same (including the case) as the Umbraco alias.

    My problem was that the case was inconsistent so it didn't work.

  • Josh Olson 79 posts 207 karma points
    Sep 22, 2013 @ 07:59
    Josh Olson
    0

    Old thread, but just wanted to say thanks Matt! I have a plethora of custom properties in my provider class, but I had been naming them differently, which then required a matching number of entries in the web.config.... very annoying. By keeping the name (and case) consistent with the Umbraco alias, I was able to avoid having to fill up my web.config and save some labor in the process.

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft