ASP.Net 配置文件:设置和使用配置文件字段时遇到困难
我目前正在尝试使用 ASP.Net 配置文件来存储有关在我们网站上注册的用户的其他用户信息。
相关示例代码:
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 中删除了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>
我尝试使用属性/add[name='FacebookUid'] 且不带;在相同的情况下得到与上面相同的错误。
我被难住了,谷歌搜索/必应却一无所获。这里有人知道可能发生什么和/或如何解决这个问题吗?非常感谢任何建设性的意见。
谢谢, 坦率
I'm currently trying to use ASP.Net Profiles to store additional user information about users that register on our site.
Relevant sample code:
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 = "";
When I leave the FacebookUid defined in the web.config; I get this error from the web.config:
Configuration Error: This profile property has already been defined.
I've double checked the web.config; that property entry only occurs once in the web.config.
I figured this is because I setup the property with the same name on the UserProfile class. I removed the properties/add[name='FacebookUid'] entry from the web.config. Once I did that; I get this error when I try to set the value of FacebookUid in Register.aspx.cs:
The settings property 'FacebookUid' was not found
Line 76: profile["FacebookUid"] = "";
I tried one more time, this time using the ProfileBase class directly:
Revised Register.aspx.cs
var profile = System.Web.Profile.ProfileBase.Create(emailAddress,true);
profile["FacebookUid"] = "";
Revised 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>
I tried this with properties/add[name='FacebookUid'] and without; got the same errors as above in the same cases.
I'm stumped and Googling/Binging has gotten me nowhere. Does anyone here have any idea what might be going on and/or how to fix this problem? Any constructive input is greatly appreciated.
Thanks,
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 web.config 中定义自定义属性以及从 ProfileBase 继承的类似乎有点矫枉过正:
http://msdn.microsoft.com/en-us /library/system.web.profile.profilebase.aspx
另外:您是否修改了 XmlProfileProvider 的代码?似乎只需要 XmlProfile 而没有其他。
http://www.java2s.com/Open-Source/ASP.NET/Asp.net-Samples/tinyproviders/Artem/Web/Security/XmlProfileProvider.cs.htm
Defining custom properties in web.config as well as a class that inherits from ProfileBase seems like overkill:
http://msdn.microsoft.com/en-us/library/system.web.profile.profilebase.aspx
Also: have you modified the code for XmlProfileProvider? It seems to expect XmlProfile and nothing else.
http://www.java2s.com/Open-Source/ASP.NET/Asp.net-Samples/tinyproviders/Artem/Web/Security/XmlProfileProvider.cs.htm