Domanda

I followed a tutorial online to use the built in asp.net profiler but it's not getting the profile. It return null everytime when running GetUserProfile:

Here is my code for invoking:

        if (UserProfile.GetUserProfile() == null)
        {
            UserProfile.Create(Membership.GetUser().UserName);
        }
        UserProfile currentProfile = UserProfile.GetUserProfile();
        currentProfile.WorksAt = "WorksAt Test";
        currentProfile.Manages = "Manages Test";
        String WorksAt = currentProfile.WorksAt;
        String Manages = currentProfile.Manages;

Here is my code for the Profile Class:

        using System.Web.Profile;
        using System.Web.Security;

        public class UserProfile : ProfileBase
        {
            [SettingsAllowAnonymous(false)]
            public string WorksAt
            {
                get { return base["WorksAt"] as string; }
                set { base["WorksAt"] = value; }
            }

            [SettingsAllowAnonymous(false)]
            public string Manages
            {
                get { return base["Manages"] as string; }
                set { base["Manages"] = value; }
            }

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

            public static UserProfile GetUserProfile()
            {
                return Create(Membership.GetUser().UserName) as UserProfile;
            }
        }
È stato utile?

Soluzione

Figure it out from a tutorial online, I needed to add inherits="UserProfile" when defining the profile provider in Web.config:

<profile defaultProvider="TestServerProfile" inherits="UserProfile">
        <providers>
            <clear/>
            <add name="TestServerProfile" type="System.Web.Profile.SqlProfileProvider" connectionStringName="TestServerConnection" applicationName="/"/>
        </providers>
  <properties>
    <group name="UserInformation">
      <add name="WorksAt" type="String" serializeAs="String" defaultValue=""  />
      <add name="Manages" type="String" serializeAs="String" defaultValue=""  />
    </group>
  </properties>
    </profile>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top