用户配置文件不起作用
本文关键字:不起作用 配置文件 用户 | 更新日期: 2023-09-27 18:33:57
我在线学习了一个教程来使用内置的 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;
这是我的配置文件类代码:
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;
}
}
从在线教程中弄清楚,在 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>