被这个问题困扰了将近一周的时间,是在是做什么事都惦记着这个问题,在网上找各种解决方案都不适合我,国外的文献看着别扭!论坛发帖咨询,大大们也只是三言两语的解答让我摸不到头脑!现在终于揭开了这个谜团,下面讲解使用membership的Profile机制扩展用户信息,一来Mark一下知识点,二来为后人提供一些力所能及的帮助!
关于微软的membership 用户机制早在.net 2.0 就开始引入了,对此我只做了简单的了解,本文不对用户机制实现原理进行解析,只用实例来叙述该如何在原有的用户字段上进行扩展字段!
在新建的项目中,membership会在我们项目中App_Data文件夹下创建一个数据库用来存储我们的用户信息已经角色权限的配置。当然我们只能在VS自带的数据库管理器中看到这个数据库,这对于我们来说很是不爽,所以在我的案例中,我选择了让数据库添加到我的SQL Server Management Studio Express 中来查看该数据库!
如何加到SQL Server Management Studio Express中呢,有两种方法:
(一) 使用VS的命令提示符:然后运行aspnet_regsql.exe来自动为我们创建用户数据库。
如下:
(二) 在C:\Windows\Microsoft.NET\Framework64\v4.0.30319目录下找到aspnet_regsql.exe文件执行即可!
以下为创建好的数据库:
接下来我们在我们的项目配置文件Web.config中来修改默认的用户数据库连接字符串(把原有自带的连接字符串删除添加如下):

1 <connectionStrings> 2 3 <add name="ApplicationServices" connectionString="DataSource=.;Initial Catalog=ProDB;User ID=sa;Password=111111;" providerName="System.Data.SqlClient" /> 4 5 </connectionStrings>
然后我们在system.web节点中去配置我们的Profile节点来扩展我们的用户字段:

1 <profile> 2 3 <!--配置信息--> 4 5 <providers> 6 7 <clear/> 8 9 <addname="AspNetSqlProfileProvider"type="System.Web.Profile.SqlProfileProvider"connectionStringName="ApplicationServices" applicationName="/" /> 10 11 </providers> 12 13 14 15 <!--新增加的用户信息字段--> 16 17 <properties> 18 19 <addname="QQ"type="String"/> 20 21 <addname="Phone"type="String"/> 22 23 <addname="Address"type="String"/> 24 25 </properties> 26 27 </profile>
在原有的AccountModels 中添加我们的用户详情类UserProfileModel:

1 ///<summary> 2 3 ///用户详细信息类 4 5 ///</summary> 6 7 publicclassUserProfileModel 8 9 { 10 11 [Display( Name = "QQ")] 12 13 publicstring QQ { get; set; } 14 15 [Display( Name = "电话")] 16 17 publicstring Phone { get; set; } 18 19 [Display (Name="地址")] 20 21 publicstring Address { get; set; } 22 23 }
在AccountController中添加我们的Action名称为:UpdateUserProfile

1 // 2 3 //Get:Account/UpdateUserProfile 4 5 6 7 publicActionResult UpdateUserProfile() 8 9 { 10 11 UserProfileModel userProfileModel = newUserProfileModel(); 12 13 try 14 15 { 16 17 userProfileModel.QQ =GetProfileItem("QQ"); 18 19 userProfileModel.Phone =GetProfileItem("Phone"); 20 21 userProfileModel.Address =GetProfileItem("Address"); 22 23 } 24 25 catch { } 26 27 returnView(userProfileModel); 28 29 } 30 31 32 33 34 35 ///<summary> 36 37 ///获取当前用户ProfileItem 38 39 ///</summary> 40 41 privatestring GetProfileItem(string key, string defaultvalue = "") 42 43 { 44 45 string value = (string)HttpContext.Profile.GetPropertyValue(key); 46 47 returnstring.IsNullOrEmpty(value)? defaultvalue : value; 48 49 } 50 51 52 53 // 54 55 //Post:Account/UpdateUserProfile 56 57 58 59 [HttpPost] 60 61 publicActionResult UpdateUserProfile(UserProfileModel userProfile) 62 63 { 64 65 try 66 67 { 68 69 HttpContext.Profile["QQ"] = userProfile.QQ; 70 71 HttpContext.Profile["Phone"] =userProfile.Phone; 72 73 HttpContext.Profile["Address"] =userProfile.Address; 74 75 return RedirectToAction("Index", "Home"); 76 77 } 78 79 catch 80 81 { 82 83 ModelState.AddModelError("Error", "用户详细信息更新出错!"); 84 85 return View(userProfile); 86 87 } 88 89 }
接来下我们只要创建强类型(UserProfileModel)视图
UpdateUserProfile即可!
我们去看看数据库中,membership是如何为我们的扩展属性进行存储的:
很有意思的是:[PropertyNames]字段是存储用户扩展属性的字段名以及配置,S表示String类型,0:4表示PropertyValuesString字段中的从第0到第2位的内容即为Address属性值。看来membershipprofile对于扩展一些用户基础信息还是很灵活的!
附录:

1 //获取当前用户Profile信息 2 3 4 5 public ActionResult Index() 6 { 7 8 ViewBag.nickname = HttpContext.Profile.GetPropertyValue(nickname); 9 10 ...... 11 12 return View(); 13 14 } 15 16 17 18 19 20 21 22 //获取指定用户Profile信息 23 24 25 26 public ActionResult Index(string user) 27 { 28 29 ProfileBase objProfile = System.Web.Profile.ProfileBase.Create(user; 30 31 ViewBag.nickname = objProfile.GetPropertyValue("nickname"); 32 33 ...... 34 35 return View(); 36 37 } 38 39 40 41 42 43 44 45 //更新当前用户 46 47 48 49 [HttpPost] 50 51 public ViewResult Index(string nickname) 52 { 53 54 HttpContext.Profile["nickname"] = nickname; 55 56 ...... 57 58 return View(); 59 60 61 62 }