ASP.NET MVC2 如何 使用 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. 
   另外, 迁移事件也是在跳转以后才触发, 只不过在跳转到业务逻辑页面之前就触发罢了. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值