Profile购物车代码片段

web.config配置profile: 
Xml代码   收藏代码
  1. ......  
  2. <connectionStrings>  
  3.     <add name="profileConnStr" connectionString="server=.\sqlexpress;database=test;uid=sa;pwd=123456"/>  
  4. </connectionStrings>  
  5. <system.web>  
  6.     <!-- 匿名用户也可访问profile -->  
  7.     <anonymousIdentification enabled="true"/>  
  8.     <profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvide">  
  9.       <providers>  
  10.         <add name="SqlProvide" connectionStringName="profileConnStr" type="System.Web.Profile.SqlProfileProvider"/>  
  11.       </providers>  
  12.       <properties>  
  13.         <add name="ShoppingCart" type="Cart" allowAnonymous="true" serializeAs="Binary"/>  
  14.       </properties>  
  15.     </profile>  
  16.     ......  
  17.      <!--  
  18.             通过 <authentication> 节可以配置 ASP.NET 用来   
  19.             识别进入用户的  
  20.             安全身份验证模式。   
  21.         -->  
  22.     <authentication mode="Forms" />  
  23. </system.web>  
  24. ......  


采用硬编码方式进行用户认证: 
C#代码   收藏代码
  1. string name = txtName.Text.Trim();  
  2. string pwd = txtPassword.Text.Trim();  
  3.   
  4. if (name == "niunan" && pwd == "123456")  
  5. {  
  6.     FormsAuthentication.SetAuthCookie(name, false);  
  7.     Response.Redirect("~/product.aspx");  
  8. }  


匿名用户的购物车数据向实名用户的购物车数据迁移 
Global.asax文件中加入如下内容: 
C#代码   收藏代码
  1. protected void Profile_MigrateAnonymous(object s, ProfileMigrateEventArgs e)  
  2. {  
  3.     ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);  
  4.     foreach (CartItem ci in anonProfile.ShoppingCart.GetItems())  
  5.     {  
  6.         Profile.ShoppingCart.AddItem(ci);  
  7.     }  
  8.   
  9.     ProfileManager.DeleteProfile(e.AnonymousID);  // 删除匿名用户的profile  
  10.     AnonymousIdentificationModule.ClearAnonymousIdentifier();  // 清除匿名用户标识  
  11.     Profile.Save();  
  12. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值