我目前正在尝试使用 ASP.Net 配置文件来存储有关在我们网站上注册的用户的其他用户信息。
相关示例代码:
用户配置文件.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;
}
}
}
网页配置
<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>
注册.aspx.cs
profile = WIF.Web.STS.Classes.UserProfile.GetUserProfile(emailAddress);
profile.FacebookUid = "";
当我离开 web.config 中定义的 FacebookUid 时;我从 web.config 得到这个错误:
Configuration Error: This profile property has already been defined.
我已经仔细检查了 web.config;该属性条目仅在 web.config 中出现一次。
我认为这是因为我在 UserProfile 类上设置了同名的属性。我从 web.config 中删除了 properties/add[name='FacebookUid'] 条目。一旦我这样做了;当我尝试在 Register.aspx.cs 中设置 FacebookUid 的值时出现此错误:
The settings property 'FacebookUid' was not found
Line 76: profile["FacebookUid"] = "";
我又试了一次,这次直接使用 ProfileBase 类:
修改后的 Register.aspx.cs
var profile = System.Web.Profile.ProfileBase.Create(emailAddress,true);
profile["FacebookUid"] = "";
修改后的 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>
我用 properties/add[name='FacebookUid'] 试过这个,但没有;在相同的情况下得到与上述相同的错误。
我被难住了,谷歌搜索/Binging 让我无处可去。这里有没有人知道可能发生了什么和/或如何解决这个问题?非常感谢任何建设性的意见。
谢谢,弗兰克