c# - ASP.Net 配置文件 : difficulty setting up and using profile fields

标签 c# asp.net profiles

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

相关示例代码:

用户配置文件.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'] 和不使用;在相同的情况下得到与上述相同的错误。

我很困惑,Google 搜索/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# - ASP.Net 配置文件 : difficulty setting up and using profile fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958533/

相关文章:

asp.net - TFS 不会删除外部 Dll

java - 不同依赖关系中的 Maven 配置文件

c# - 无法使用 JSON.net 从 stats.nba 下载 JSON 文件

c# - 为什么 'public event EventHandler cccc' 为空?

c# - LINQ 左连接可空值

c# - 如何在 C# 中存储函数指针

c# - 从 .NET Core 项目引用可移植类库

c# - 单例和 HttpApplicationState

Angular 2 配置文件和 tomcat

java - Spring 轮廓组