Profile购物车代码片段

web.config配置profile:

......
<connectionStrings>
<add name="profileConnStr" connectionString="server=.\sqlexpress;database=test;uid=sa;pwd=123456"/>
</connectionStrings>
<system.web>
<!-- 匿名用户也可访问profile -->
<anonymousIdentification enabled="true"/>
<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvide">
<providers>
<add name="SqlProvide" connectionStringName="profileConnStr" type="System.Web.Profile.SqlProfileProvider"/>
</providers>
<properties>
<add name="ShoppingCart" type="Cart" allowAnonymous="true" serializeAs="Binary"/>
</properties>
</profile>
......
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Forms" />
</system.web>
......


采用硬编码方式进行用户认证:

string name = txtName.Text.Trim();
string pwd = txtPassword.Text.Trim();

if (name == "niunan" && pwd == "123456")
{
FormsAuthentication.SetAuthCookie(name, false);
Response.Redirect("~/product.aspx");
}


匿名用户的购物车数据向实名用户的购物车数据迁移
Global.asax文件中加入如下内容:

protected void Profile_MigrateAnonymous(object s, ProfileMigrateEventArgs e)
{
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
foreach (CartItem ci in anonProfile.ShoppingCart.GetItems())
{
Profile.ShoppingCart.AddItem(ci);
}

ProfileManager.DeleteProfile(e.AnonymousID); // 删除匿名用户的profile
AnonymousIdentificationModule.ClearAnonymousIdentifier(); // 清除匿名用户标识
Profile.Save();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值