加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:01:05 所属栏目:百科 来源:网络整理
导读:我目前正在尝试使用ASP.Net配置文件来存储有关在我们网站上注册的用户的其他用户信息. 相关示例代码: UserProfile.cs public class UserProfile: System.Web.Profile.ProfileBase{ public static UserProfile GetUserProfile(string username) { return Cre
我目前正在尝试使用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中删除了属性/ 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让我无处可去.这里有没有人知道可能会发生什么和/或如何解决这个问题?非常感谢任何建设性的意见.

谢谢,
坦率

解决方法

在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读