c# – ASP.Net配置文件:设置和使用配置文件字段时遇到困难

前端之家收集整理的这篇文章主要介绍了c# – ASP.Net配置文件:设置和使用配置文件字段时遇到困难前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在尝试使用ASP.Net配置文件来存储有关在我们网站上注册用户的其他用户信息.

相关示例代码

UserProfile.cs

  1. public class UserProfile: System.Web.Profile.ProfileBase
  2. {
  3.  
  4. public static UserProfile GetUserProfile(string username)
  5. {
  6. return Create(username) as UserProfile;
  7. }
  8.  
  9. public static UserProfile GetUserProfile()
  10. {
  11. return GetUserProfile(Membership.GetUser().UserName);
  12. }
  13.  
  14. public string FacebookUid
  15. {
  16. get
  17. {
  18. return base["FacebookUid"] as string;
  19. }
  20. set
  21. {
  22. base["FacebookUid"] = value;
  23. }
  24. }
  25. }

Web.config文件

  1. <profile enabled="true" inherits="WIF.Web.STS.Classes.UserProfile" defaultProvider="XmlProfileProvider" >
  2. <properties>
  3. <add name="FacebookUid" />
  4. </properties>
  5. <providers>
  6. <add name="XmlProfileProvider" type="Artem.Web.Security.XmlProfileProvider,Artem.Web.Security.Xml" applicationName="/" fileName="Profiles.xml" folder="~/App_Data/"/>
  7. </providers>
  8. </profile>

Register.aspx.cs

  1. profile = WIF.Web.STS.Classes.UserProfile.GetUserProfile(emailAddress);
  2. profile.FacebookUid = "";

当我离开web.config中定义的FacebookUid时;我从web.config得到这个错误

  1. Configuration Error: This profile property has already been defined.

我已经仔细检查了web.config;该属性条目仅在web.config中出现一次.

我想这是因为我在UserProfile类上设置了具有相同名称属性.我从web.config中删除属性/ add [name =’FacebookUid’]条目.一旦我做到了;当我尝试在Register.aspx.cs中设置FacebookUid的值时出现此错误

  1. The settings property 'FacebookUid' was not found
  2. Line 76: profile["FacebookUid"] = "";

我再试一次,这次直接使用ProfileBase类:

修订了Register.aspx.cs

  1. var profile = System.Web.Profile.ProfileBase.Create(emailAddress,true);
  2. profile["FacebookUid"] = "";

修改了web.config:

  1. <profile enabled="true" defaultProvider="XmlProfileProvider" >
  2. <properties>
  3. <add name="FacebookUid" />
  4. </properties>
  5. <providers>
  6. <add name="XmlProfileProvider" type="Artem.Web.Security.XmlProfileProvider,Artem.Web.Security.Xml" applicationName="/" fileName="Profiles.xml" folder="~/App_Data/"/>
  7. </providers>
  8. </profile>

我尝试使用properties / add [name =’FacebookUid’]而没有;在相同的情况下得到与上述相同的错误.

我很难过,谷歌/ Binging让我无处可去.这里有没有人知道可能会发生什么和/或如何解决这个问题?非常感谢任何建设性的意见.

谢谢,
坦率

解决方法

在web.config中定义自定义属性以及从ProfileBase继承的类似乎有点矫枉过正:

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

另外:你修改过XmlProfileProvider的代码吗?它似乎期待XmlProfile,没有别的.

http://www.java2s.com/Open-Source/ASP.NET/Asp.net-Samples/tinyproviders/Artem/Web/Security/XmlProfileProvider.cs.htm

猜你在找的C#相关文章