将附加数据与成员数据一起存储在 aspnet_profile 表中
我在基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过在 web.config 中进行一些与默认配置文件提供程序名称相关的更改来使其工作:
此外,我在调用 ProfileBase.Create 函数和设置 Profile.FullName 之间添加了一行;
我终于在 aspnet_profile 表中看到了新注册用户的条目:)
I made it work by making some changes related to default profile provider name in the web.config:
Also I added one more line between call to ProfileBase.Create function and setting the Profile.FullName;
I finally saw an entry in the aspnet_profile table for the newly registered user :)
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
使用 ASP.NET 配置文件提供程序时,配置文件属性是自定义的方式用户个人资料。但是,您不必自己创建和保存配置文件实例 - 它是由 ASp.NET 运行时自行完成的。使用 HttpContext.Current.Profile 或使用页面本身的强类型动态
Profile
属性。通过稍后使用,您可以编写诸如不需要调用
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 asThere 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 adynamic
variable for more readable code as done by you). Another way is to write your own class.