相关示例代码:
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 = "";
当我离开web.config中定义的FacebookUid时;我从web.config得到这个错误:
Configuration Error: This profile property has already been defined.
我已经仔细检查了web.config;该属性条目仅在web.config中出现一次.
我想这是因为我在UserProfile类上设置了具有相同名称的属性.我从web.config中删除了属性/ 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让我无处可去.这里有没有人知道可能会发生什么和/或如何解决这个问题?非常感谢任何建设性的意见.
谢谢,
坦率
解决方法
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