ASP.NET MVC2 如何 使用 Profile 的问题及解决
2013-02-03 07:12
387 查看
需求
问题很简单, 希望用户注册登陆之后可以立即在其profile里面添加一个字符串信息: SubCompany.
配置
为了使用profile, 首先要在web.config中进行配置:
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="subCompany" type="String" allowAnonymous="true" />
</properties>
</profile>
提交信息
注册表单中提交到register action之后, 基本信息还好办, 可以使用asp.net 2.0时就提供的membership机制进行用户的创建:
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
再进行登录:
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
此时cookie, 也就是formsauthentication的ticket已经成功设置了, 不过 此时 的User.Identity.IsAuthenticated 仍然是false ,这是因为创建的cookie还在客户端上,
需要下一次的http提交才能到达服务器, 系统才能辨识身份,此时的profile仍然是匿名用户的(如果允许匿名用户有profile的话), 这里也谈到这个问题, 当然redirecttoAction一次之后便可以识别了.
所以很容易想到的一个直接方法是redirect到另一个action, 把subCompany的值传过去, 在其中负责profile的信息的更改.不过一般注册成功登录后会自动进行跳转到之前的操作页面, 也就是还要继续跳到业务逻辑需要的地方, 无形中比不使用profile多了一次redirect.
当然我们想要直接立即设置Profile.subCompany也可以, 这取决于是否基于匿名的profile, 就是没有登录也可以存储一些信息在cookie, 这需要在web.config中进行设置:
<anonymousIdentification enabled="true"/>
用户迁移
在设置了匿名身份的情况下, 实际上我们需要把匿名用户的信息迁移到注册用户profile里面去, asp.net给我们提供了一个事件, 来进行匿名用户到注册用户的迁移 .更多资料 http://www.52mvc.com/net/list.aspx
在global.asax.cs中添加事件处理函数, 可在第一次跳转之后自动触发:
protected void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe){
var anonymousProfile = CustomProfile.GetProfile(pe.AnonymousID); //取之前的匿名ID
var nowProfile = CustomProfile.GetProfile();//取当前的profile
nowProfile.SubComanyName = anonymousProfile.SubComanyName; //赋值即可
// 删除匿名用户的profile 和其cookie
ProfileManager.DeleteProfile(pe.AnonymousID);
AnonymousIdentificationModule.ClearAnonymousIdentifier();
// 删除匿名用户
Membership.DeleteUser(pe.AnonymousID, true);
}
所以登陆后立即设置profile 信息的位置应该在global.asax中. CustomProfile是一个自定义的类, 下面将会介绍它.
自定义Profile类
如果没有打开匿名身份profile, 那么简单一些, 只要直接获取username对应的profile修改,保存即可, 未跳转之前这涉及到获取其他用户的profile, 因为未跳转之前目前仍是匿名身份, 是不能通过httpcontext.profile来获得的.
不过ASP.NET MVC2比较烦人, 其仍然没有profile.GetProfile(string)这样的函数, 也不会自动生成ProfileCommon这样的辅助类, 带来两个问题是: 1 不能方便的获取其他用户的profile. 2, profile中的属性没有强类型的支持, 不过这2个问题可以通过手动来解决:
首先自己写一个profile类:
public class CustomProfile : ProfileBase{
public virtual string SubComanyName{
get { return ((string)(this.GetPropertyValue("subCompany"))); }
set { this.SetPropertyValue("subCompany", value); }
}
public static CustomProfile GetProfile(){
return (CustomProfile) HttpContext.Current.Profile;
}
public static CustomProfile GetProfile(string userName){
return (CustomProfile) Create(userName);
}
}
其实可以看出, ProfileBase.Create(string) 并非新建, 实则就是load入其他用户的profile, 不过名字起得吓点人罢了. 所以也可以不用自定义这个类, 用基本的api进行profile的操作, 只是稍微有点罗嗦.
然后web.config,添加粗体配置:
<profile inherits="ForeignBussinessManager.Models.CustomProfile">
...
</profile>
那么可以建立完用户之后, 直接修改它的profile了:
var p = CustomProfile.GetProfile(model.UserName);
p.SubComanyName = model.SubCompanyName;
p.Save();
总结
两种情况, 一是匿名用户profile开启时候, 使用global的用户迁移事件, 可以有效地控制匿名cookie, 减少不必要的浪费. 如果需要把匿名用户cookie关了呢? 就直接修改其他用户的profile.
另外, 迁移事件也是在跳转以后才触发, 只不过在跳转到业务逻辑页面之前就触发罢了.
问题很简单, 希望用户注册登陆之后可以立即在其profile里面添加一个字符串信息: SubCompany.
配置
为了使用profile, 首先要在web.config中进行配置:
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="subCompany" type="String" allowAnonymous="true" />
</properties>
</profile>
提交信息
注册表单中提交到register action之后, 基本信息还好办, 可以使用asp.net 2.0时就提供的membership机制进行用户的创建:
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
再进行登录:
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
此时cookie, 也就是formsauthentication的ticket已经成功设置了, 不过 此时 的User.Identity.IsAuthenticated 仍然是false ,这是因为创建的cookie还在客户端上,
需要下一次的http提交才能到达服务器, 系统才能辨识身份,此时的profile仍然是匿名用户的(如果允许匿名用户有profile的话), 这里也谈到这个问题, 当然redirecttoAction一次之后便可以识别了.
所以很容易想到的一个直接方法是redirect到另一个action, 把subCompany的值传过去, 在其中负责profile的信息的更改.不过一般注册成功登录后会自动进行跳转到之前的操作页面, 也就是还要继续跳到业务逻辑需要的地方, 无形中比不使用profile多了一次redirect.
当然我们想要直接立即设置Profile.subCompany也可以, 这取决于是否基于匿名的profile, 就是没有登录也可以存储一些信息在cookie, 这需要在web.config中进行设置:
<anonymousIdentification enabled="true"/>
用户迁移
在设置了匿名身份的情况下, 实际上我们需要把匿名用户的信息迁移到注册用户profile里面去, asp.net给我们提供了一个事件, 来进行匿名用户到注册用户的迁移 .更多资料 http://www.52mvc.com/net/list.aspx
在global.asax.cs中添加事件处理函数, 可在第一次跳转之后自动触发:
protected void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe){
var anonymousProfile = CustomProfile.GetProfile(pe.AnonymousID); //取之前的匿名ID
var nowProfile = CustomProfile.GetProfile();//取当前的profile
nowProfile.SubComanyName = anonymousProfile.SubComanyName; //赋值即可
// 删除匿名用户的profile 和其cookie
ProfileManager.DeleteProfile(pe.AnonymousID);
AnonymousIdentificationModule.ClearAnonymousIdentifier();
// 删除匿名用户
Membership.DeleteUser(pe.AnonymousID, true);
}
所以登陆后立即设置profile 信息的位置应该在global.asax中. CustomProfile是一个自定义的类, 下面将会介绍它.
自定义Profile类
如果没有打开匿名身份profile, 那么简单一些, 只要直接获取username对应的profile修改,保存即可, 未跳转之前这涉及到获取其他用户的profile, 因为未跳转之前目前仍是匿名身份, 是不能通过httpcontext.profile来获得的.
不过ASP.NET MVC2比较烦人, 其仍然没有profile.GetProfile(string)这样的函数, 也不会自动生成ProfileCommon这样的辅助类, 带来两个问题是: 1 不能方便的获取其他用户的profile. 2, profile中的属性没有强类型的支持, 不过这2个问题可以通过手动来解决:
首先自己写一个profile类:
public class CustomProfile : ProfileBase{
public virtual string SubComanyName{
get { return ((string)(this.GetPropertyValue("subCompany"))); }
set { this.SetPropertyValue("subCompany", value); }
}
public static CustomProfile GetProfile(){
return (CustomProfile) HttpContext.Current.Profile;
}
public static CustomProfile GetProfile(string userName){
return (CustomProfile) Create(userName);
}
}
其实可以看出, ProfileBase.Create(string) 并非新建, 实则就是load入其他用户的profile, 不过名字起得吓点人罢了. 所以也可以不用自定义这个类, 用基本的api进行profile的操作, 只是稍微有点罗嗦.
然后web.config,添加粗体配置:
<profile inherits="ForeignBussinessManager.Models.CustomProfile">
...
</profile>
那么可以建立完用户之后, 直接修改它的profile了:
var p = CustomProfile.GetProfile(model.UserName);
p.SubComanyName = model.SubCompanyName;
p.Save();
总结
两种情况, 一是匿名用户profile开启时候, 使用global的用户迁移事件, 可以有效地控制匿名cookie, 减少不必要的浪费. 如果需要把匿名用户cookie关了呢? 就直接修改其他用户的profile.
另外, 迁移事件也是在跳转以后才触发, 只不过在跳转到业务逻辑页面之前就触发罢了.
相关文章推荐
- 如何解决烦人的VS.NET2003编译时“无法将程序集复制到文件,另一个程序正在使用,进程无法访问”的问题?
- 如何解决在ie下,Echarts多次使用setOption更改数据时,数据错乱问题
- WebPack系列:Webpack编译的代码如何在tomcat中使用时静态资源路径不对的问题如何解决
- 教你如何解决Sublime Text 3使用中出现的中文乱码问题
- 如何解决在excel工作表中鼠标的右键不能使用的问题
- 如何解决在ie下,Echarts多次使用setOption更改数据时,数据错乱问题
- Win7如何手动注册DLL文件以解决程序不能使用的问题
- 解决在GitHubDesktop中如何使用atom打开github仓库的问题
- 移动WEB布局使用less语法之视网膜屏幕如何来解决图片像素问题
- 如何使用 TRACERT 解决 Windows 中的 TCP/IP 问题
- 如何解决python中使用flask时遇到的markupsafe._compat包缺失的问题
- 如何解决eWebEditor在线编辑器的远程自动上传功能不能使用的问题?
- 解决PasswordBox的Password非依赖属性,而无法绑定的问题,以及常规命令如何使用
- python中django使用MySQL时出问题如何解决
- 如何使用 ADPlus 解决“挂起”和“崩溃”问题
- ASP.Net下如何解决关于Access数据库“操作必须使用一个可更新的查询”问题
- [ChneChen的随笔][管理之道]什么才是我们需要的解决问题的方法(从如何限制公司电脑使用U盘解决之道谈起)
- [完美解决]如何在windows安装docker toolbox,使用tensorflow,Jupyter Notebook,各种问题的解决方案
- 看看如何解决“SQL Server只能使用Windows身份登录,不能使用sa等Sql server身份进行登录”的问题
- 图形验证码如何解决集群中使用问题