【问题标题】:ASP.Net profiles: difficulty setting up and using profile fieldsASP.Net 配置文件:设置和使用配置文件字段的困难
【发布时间】:2011-12-19 00:02:06
【问题描述】:

我目前正在尝试使用 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>

注册.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 让我无处可去。这里有没有人知道可能发生了什么和/或如何解决这个问题?非常感谢任何建设性的意见。

谢谢, 弗兰克

【问题讨论】:

    标签: c# asp.net profiles


    【解决方案1】:

    在 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

    【讨论】:

    • 正如所指出的,您通常只会在 web.config 或自定义类中定义属性(我更喜欢这种方法来进行强类型访问)。如果您从 web.config 中删除属性定义,那么该错误应该会消失。
    • 如上所述;我尝试专门使用自定义配置文件类。发生的情况是存储配置文件字段的内部哈希没有我定义的配置文件字段的条目。例如: UserProfile.Firstname 在 base["Firstname"] 中没有条目;因此,当您尝试为其设置值时,它会引发错误。
    • 正如 sq33G 所暗示的那样;这确实是 XmlProfileProvider 的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2014-10-14
    相关资源
    最近更新 更多