ASP.Net profiles: difficulty setting up and using profile fields
Asked Answered
W

1

7

I'm currently trying to use ASP.Net Profiles to store additional user information about users that register on our site.

Relevant sample code:

UserProfile.cs

public class UserProfile: System.Web.Profile.ProfileBase
{

    public static UserProfile GetUserProfile(string username)
    {
        return Create(username) as UserProfile;
    }

    public static UserProfile GetUserProfile()
    {
        return GetUserProfile(Membership.GetUser().UserName);
    }

    public string FacebookUid
    {
        get
        {
            return base["FacebookUid"] as string;
        }
        set
        {
            base["FacebookUid"] = value;
        }
    }
}

Web.config

  <profile enabled="true" inherits="WIF.Web.STS.Classes.UserProfile" defaultProvider="XmlProfileProvider" >
      <properties>
          <add name="FacebookUid" />
      </properties>
      <providers>
          <add name="XmlProfileProvider" type="Artem.Web.Security.XmlProfileProvider, Artem.Web.Security.Xml" applicationName="/" fileName="Profiles.xml" folder="~/App_Data/"/>
      </providers>
  </profile>

Register.aspx.cs

profile = WIF.Web.STS.Classes.UserProfile.GetUserProfile(emailAddress);
    profile.FacebookUid = "";

When I leave the FacebookUid defined in the web.config; I get this error from the web.config:

  Configuration Error: This profile property has already been defined.

I've double checked the web.config; that property entry only occurs once in the web.config.

I figured this is because I setup the property with the same name on the UserProfile class. I removed the properties/add[name='FacebookUid'] entry from the web.config. Once I did that; I get this error when I try to set the value of FacebookUid in Register.aspx.cs:

  The settings property 'FacebookUid' was not found
  Line 76:             profile["FacebookUid"] = "";

I tried one more time, this time using the ProfileBase class directly:

Revised Register.aspx.cs

        var profile = System.Web.Profile.ProfileBase.Create(emailAddress,true);
        profile["FacebookUid"] = "";

Revised web.config:

<profile enabled="true" defaultProvider="XmlProfileProvider" >
          <properties>
              <add name="FacebookUid" />
          </properties>
          <providers>
              <add name="XmlProfileProvider" type="Artem.Web.Security.XmlProfileProvider, Artem.Web.Security.Xml" applicationName="/" fileName="Profiles.xml" folder="~/App_Data/"/>
          </providers>
      </profile>

I tried this with properties/add[name='FacebookUid'] and without; got the same errors as above in the same cases.

I'm stumped and Googling/Binging has gotten me nowhere. Does anyone here have any idea what might be going on and/or how to fix this problem? Any constructive input is greatly appreciated.

Thanks, Frank

Wantage answered 31/10, 2011 at 19:17 Comment(0)
C
6

Defining custom properties in web.config as well as a class that inherits from ProfileBase seems like overkill:

Notes to Inheritors

You can create a custom profile implementation that inherits from the ProfileBase abstract class and defines properties for the user profile that are not specified in the profile configuration element.

http://msdn.microsoft.com/en-us/library/system.web.profile.profilebase.aspx

Also: have you modified the code for XmlProfileProvider? It seems to expect XmlProfile and nothing else.

http://www.java2s.com/Open-Source/ASP.NET/Asp.net-Samples/tinyproviders/Artem/Web/Security/XmlProfileProvider.cs.htm

Corettacorette answered 31/10, 2011 at 19:50 Comment(3)
As pointed out you would usually only define the property in either the web.config or in your custom class (I prefer this method for strong typed access). If you remove the property definition from your web.config then that error should go away.Syncarpous
As I mentioned above; I tried going with the Custom Profile class exclusively. What happens is the internal Hash that stores the profile fields doesn't have entries for the profile fields I defined. For example: UserProfile.Firstname doesn't have an entry in base["Firstname"]; so it throws an error when you attempt to set the value for it.Wantage
As sq33G alluded to; this was indeed an issue with the XmlProfileProvider.Wantage

© 2022 - 2024 — McMap. All rights reserved.