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

发布于 2024-12-13 03:12:33 字数 2628 浏览 4 评论 0原文

我目前正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-12-20 03:12:33

在 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

Defining custom properties in web.config as well as a class that inherits from ProfileBase seems like overkill:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文