Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 735
Frank Rosario Avatar asked Oct 31 '11 19:10

Frank Rosario


1 Answers

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

like image 104
sq33G Avatar answered Sep 28 '22 05:09

sq33G