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(); }
页:
[1]