UserProfile无法正常工作

时间:2012-05-10 15:16:00

标签: c# asp.net asp.net-profiles

我在线阅读了一个教程,使用内置的asp.net探查器,但它没有得到个人资料。每次运行GetUserProfile时都返回null:

这是我的调用代码:

        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;

以下是我的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;
            }
        }

1 个答案:

答案 0 :(得分:1)

从在线教程中了解到,我需要在Web.config中定义配置文件提供程序时添加inherits =“UserProfile”:

<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>