Web应用程序项目 - 如何使用ProfileCommon

Jon*_*Jon 6 c# asp.net profilecommon

我正在将我在旧盒子上开发的网站移植到新的开发环境中.我还没有复制所有文件,因为我没有一个很好的文件结构,并且我需要删除一些代码部分.

最初我创建了一个网站(文件 - >新建 - >网站).我想要一个文件结构,如:

用于构建的流行文件夹结构

所以我创建了一个新的空白解决方案,所以sln文件是独立的,然后添加项目(各种DLL项目)和ASP.NET Web应用程序.

这最后一部分似乎给我带来了一些问题,我现在收到以下错误:

"无法找到类型或命名空间名称'ProfileCommon'".

我找到了以下页面:

http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx

这似乎有点长,我希望有人可能知道更好的解决方案.

我试图将ProfileCommon与CreateUser向导一起使用,因为我在其中添加了一些额外的信息.

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    // Create an empty Profile for the newly created user
    ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

    // Populate some Profile properties off of the create user wizard
    p.CurrentLevel = Int32.Parse(((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("clevel")).SelectedValue);

    // Save profile - must be done since we explicitly created it
    p.Save();
}
Run Code Online (Sandbox Code Playgroud)

Web.config文件:

<profile enabled="true">
<properties>
    <add name="CurrentLevel" type="Int32"/>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)

如果有另一种方法可以将这些额外信息添加到创建向导中,或者只是更好地为新用户设置额外信息,那么我很满意,非常感激.

感谢您的帮助和建议.

jp2*_*ode 5

这是一个非常晚的帖子,但是当我将VB.NET Visual Studio 2008(.NET 3.5)网站移植到C#Visual Studio 2010(.NET 4.0)网站时,我遇到了同样的问题.

我ProfileCommon在MSDN的ProfileBase文档中找到了引用,但没有提到如何获取该对象.

从你有用的MSDN链接,我注意到它ProfileCommon只会是一个包装器HttpContext.

简而言之,我使用var关键字从中提取ProfileCommon信息HttpContext,如:

var profile = HttpContext.Current.Profile;
Run Code Online (Sandbox Code Playgroud)

使用这一点信息,我能够创建整个类来读取和写入我的网站访问者的信息.

和你一样,我希望这段代码可以帮助别人:

using System.Web;
using System.Web.Security;

namespace WebApplication17 {

  public partial class ManageProfile : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
      if (!IsPostBack) {
        if (User.Identity.IsAuthenticated) {
          loadProfile();
        } else {
          goHome();
        }
      }
    }

    private void changePassword(string pwdOld, string pwdNew) {
      MembershipUser user = Membership.GetUser(User.Identity.Name);
      user.ChangePassword(pwdOld, pwdNew);
      Membership.UpdateUser(user);
    }

    private void goHome() {
      Server.Transfer("Default.aspx");
    }

    private void loadProfile() {
      MembershipUser user = Membership.GetUser(User.Identity.Name);
      txtEmail.Text = user.Email;
      TextBox3.Text = user.GetPassword();
      var profile = HttpContext.Current.Profile;
      txtTitle.Text = profile.GetPropertyValue("Title").ToString();
      txtName.Text = profile.GetPropertyValue("Name").ToString();
      txtAddress.Text = profile.GetPropertyValue("Address").ToString();
      txtCity.Text = profile.GetPropertyValue("City").ToString();
      txtSt.Text = profile.GetPropertyValue("St").ToString();
      txtZip.Text = profile.GetPropertyValue("Zip").ToString();
      txtPhone.Text = profile.GetPropertyValue("Phone").ToString();
      txtFax.Text = profile.GetPropertyValue("Fax").ToString();
      txtCompany.Text = profile.GetPropertyValue("Company").ToString();
    }

    private void setProfile() {
      MembershipUser user = Membership.GetUser(User.Identity.Name);
      user.Email = txtEmail.Text;
      Membership.UpdateUser(user);
      var profile = HttpContext.Current.Profile;
      profile.SetPropertyValue("Title", txtTitle.Text);
      profile.SetPropertyValue("Name", txtName.Text);
      profile.SetPropertyValue("Address", txtAddress.Text);
      profile.SetPropertyValue("City", txtCity.Text);
      profile.SetPropertyValue("St", txtSt.Text);
      profile.SetPropertyValue("Zip", txtZip.Text);
      profile.SetPropertyValue("Phone", txtPhone.Text);
      profile.SetPropertyValue("Fax", txtFax.Text);
      profile.SetPropertyValue("Company", txtCompany.Text);
      profile.Save();
    }

    protected void Button6_Click(object sender, EventArgs e) {
      changePassword(TextBox3.Text, TextBox4.Text);
      goHome();
    }

    protected void Button11_Click(object sender, EventArgs e) {
      setProfile();
      goHome();
    }

  }

}
Run Code Online (Sandbox Code Playgroud)

  • @Keith,使用`ProfileBase.Create(string username)`函数获取特定用户的配置文件. (2认同)

Jon*_*Jon 4

我找到了这个问题的“一个”解决方案。不确定这是否是最好的,但它适合我的情况。需要最少的代码更改。

http://msdn.microsoft.com/en-us/library/aa983476.aspx

希望它对其他人有帮助(或者当我再次忘记时对我有帮助)。