将附加数据与成员数据一起存储在 aspnet_profile 表中

发布于 2024-12-11 04:01:15 字数 1276 浏览 0 评论 0原文

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

我不确定,但我认为 asp.net 中的配置文件系统是为这种类型的需求而构建的。另外,我还看到一个包含其他名为“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 表中添加了任何行。另外,我想问这是否是添加附加数据和默认会员数据的首选方式

I am using Membership API with OpenId implementation in my project built on MVC2 framework.
Other than the username , I need some other fields to be associated with the user at the time of registration.

I am not sure though but I think Profile system in asp.net is built for this type of requirement. Also, I see a table with other membership tables named 'aspnet_profile'.

I added following settings in the application web.config to enable the profile :

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

      </properties>

    </profile>

As said before, application need some additional data to be associated with the user, so when creating a user using Membership API, I added few more lines of code for making entry into profile table

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");



                   }

But I don't see any line added in the aspnet_profile table in the database. Also, I wanted to ask if this is the preferred way of adding additional data along with default membership data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

娜些时光,永不杰束 2024-12-18 04:01:15

我通过在 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 表中看到了新注册用户的条目:)

I made it work by making some changes related to default profile provider name in the 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>

Also I added one more line between call to ProfileBase.Create function and setting the Profile.FullName;

profile.Initialize(userModel.userName, true);

I finally saw an entry in the aspnet_profile table for the newly registered user :)

虫児飞 2024-12-18 04:01:15

1,您需要创建一个配置文件类来定义配置文件结构

2,您需要将 web.config 中的配置文件设置配置为

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

您只需要在使用它之前执行前 2 个步骤。

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

1, you need to create a profile class to define the profile structure

2, you need to config your profile settings in web.config as

3, now you can use your code now.

you just need to do the first 2 steps before you using it.

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

江南烟雨〆相思醉 2024-12-18 04:01:15

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

Profile.UserName = "User Name"; 

不需要调用 Save 方法的代码。有关详细信息,请参阅本文

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

While using ASP.NET profile provider, profile properties is the way to customize the user profile. However, you don't have to create and save profile instance by yourself - it is done by ASp.NET runtime it-self. Use either HttpContext.Current.Profile or use strongly typed dynamic Profile property from the page it self. By using later, you can write code such as

Profile.UserName = "User Name"; 

There is no need to call Save method. For more information see this article.

In a web application, one cannot really refer to dynamically created Profile class, so you have to work with HttpContext.Current.Profile (you can of course assign it to a dynamic variable for more readable code as done by you). Another way is to write your own class.

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