【问题标题】:Storing additional data in aspnet_profile table with Membership data使用会员数据在 aspnet_profile 表中存储附加数据
【发布时间】:2011-12-12 07:09:45
【问题描述】:

我在基于 MVC2 框架的项目中使用具有 OpenId 实现的 Membership API。 除了用户名之外,我还需要在注册时与用户关联一些其他字段。

虽然我不确定,但我认为 asp.net 中的 Profile 系统是为这种类型的需求而构建的。此外,我还看到一个包含其他名为“aspnet_profile”的成员资格表的表。

我在应用程序 web.config 中添加了以下设置以启用配置文件:

<profile enabled="true">
      <properties>
        <add  name="FullName" allowAnonymous="false"/>

      </properties>

    </profile>

如前所述,应用程序需要一些额外的数据来与用户关联,所以在使用 Membership API 创建用户时,我添加了几行代码来进入配置文件表

System.Web.Security.MembershipCreateStatus status = MembershipService.CreateUser(userModel.UserName, userModel.Password, userModel.UserName);

                   if (status == System.Web.Security.MembershipCreateStatus.Success)
                   {
                       FormsService.SignIn(userModel.UserName, true);
                       Session["Username"] = userModel.UserName;

                       dynamic profile = ProfileBase.Create(MembershipService.GetUser(userModel.UserName).UserName);
                       profile.FullName = userModel.UserFullName;
                       profile.Save();

                       RedirectToAction("Tech", "Home");



                   }

但我没有看到数据库的 aspnet_profile 表中添加了任何行。另外,我想问一下这是否是添加附加数据和默认会员数据的首选方式

【问题讨论】:

    标签: asp.net .net asp.net-membership asp.net-profiles


    【解决方案1】:

    1、需要创建profile类来定义profile结构

    2、你需要在web.config中配置你的profile设置为

    3,现在你可以使用你的代码了。

    您只需在使用前完成前 2 个步骤。

    参考:http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx

    【讨论】:

    • 我不想为此创建一个单独的配置文件类。我想我缺少 web.config 中的设置。我认为没有必要创建一个配置文件类来完成这项工作
    【解决方案2】:

    在使用 ASP.NET 配置文件提供程序时,profile properties 是自定义用户配置文件的方式。但是,您不必自己创建和保存配置文件实例 - 它由 ASp.NET 运行时自行完成。使用HttpContext.Current.Profile 或使用其自身页面中的强类型动态Profile 属性。以后使用,可以写出诸如

    之类的代码
    Profile.UserName = "User Name"; 
    

    无需调用Save 方法。有关更多信息,请参阅this article

    在 Web 应用程序中,不能真正引用动态创建的 Profile 类,因此您必须使用 HttpContext.Current.Profile(当然,您可以将其分配给 dynamic 变量,以获得更易读的代码,就像您所做的那样)。另一种方法是写your own class

    【讨论】:

    • MVC 没有给你 HttpContext.Current ,你可以直接使用 HttpContext.Profile 访问profile类
    • @Kunal,HttpContext.Profile 是一个实例属性,因此您需要一个 HttpContext 实例,通常可以在任何 ASP.NET 应用程序(包括 ASP.NET MVC)中通过HttpContext.Current 获得该实例!跨度>
    • @VinayC 他的意思是说,在一个MVC ControllerView 类中,有一个HttpContext 属性,它基本上是HttpContext.Current 的一个包装器。
    【解决方案3】:

    我通过在 web.config 中对默认配置文件提供程序名称进行一些更改来使其工作:

    <profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
          <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider" applicationName="/" connectionStringName="ApplicationServices" type="System.Web.Profile.SqlProfileProvider" />
          </providers>
    
          <properties>
            <add  name="FullName" allowAnonymous="false"/>
    
          </properties>
    
        </profile>
    

    我还在调用 ProfileBase.Create 函数和设置 Profile.FullName 之间又添加了一行;

    profile.Initialize(userModel.userName, true);
    

    我终于在 aspnet_profile 表中看到了新注册用户的条目:)

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 2020-01-07
      • 1970-01-01
      相关资源
      最近更新 更多