ASP.Net配置文件:设置和使用配置文件字段的难点

7

我目前正在尝试使用ASP.Net Profiles来存储注册在我们网站上的用户的其他信息。

相关示例代码:

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>

我尝试使用properties/add[name='FacebookUid']和不使用,但在相同情况下出现了与上述相同的错误。我陷入了困境,谷歌和必应搜索没有帮助,这里是否有人知道可能发生了什么,或者如何解决这个问题?任何建设性意见都将不胜感激。
谢谢, Frank
1个回答

6

在web.config中定义自定义属性以及继承自ProfileBase的类似于过度设计:

继承者注意事项

您可以创建一个自定义配置文件实现,该实现继承自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


1
正如所指出的,通常只需要在web.config或自定义类中定义属性(我更喜欢这种方法来实现强类型访问)。如果您从web.config中删除属性定义,则该错误应该消失。 - Gary.S
1
正如我上面提到的,我尝试仅使用自定义配置文件类。问题在于存储配置文件字段的内部哈希表没有我定义的配置文件字段的条目。例如:UserProfile.Firstname在base["Firstname"]中没有条目;因此,在您尝试设置其值时会引发错误。 - Frank Rosario
正如sq33G所暗示的那样,这确实是XmlProfileProvider存在的问题。 - Frank Rosario

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接