修改cookie

修改目标:以前登陆Cookie保存选项只能选择“永久保存”和“不保存”两种情况,
要将其修改为可选择“不保存”、“保存一天”、“保存一个月”、“保存一年”四种可选情况


修改源码:宝玉汉化版、


涉及部分:皮肤的修改,自定义控件的修改、新增派生自DropDownList的一个类、皮肤XML修改


第一部分:修改皮肤XML
  在ASP.NET Forums的WEB/Languages存放着皮肤上使用的不同语言文字,我们需要修改/zh-CN目录
下的Resources.xml,查找

 
<resource name = "LoginSmall_AutoLogin">自动登录</resource>
 

 

找到后屏蔽掉,增加


 
<resource name="LoginSmall_AutoLogin01">不保存</resource><resource name="LoginSmall_AutoLogin02">保存一天</resource><resource

name="LoginSmall_AutoLogin03">保存一月</resource><resource name="LoginSmall_AutoLogin04">保存一年</resource>
 

 

第二部分:新增一个派生自DropDownList的一个类
 
public class CookieDropDownList : DropDownList { public CookieDropDownList() {

//在ResourceManager.GetString中读取了Resources.xml,对怎么对XML进行读取的朋友可以看看   
  Items.Add(new ListItem( ResourceManager.GetString("LoginSmall_AutoLogin01")));    
  Items.Add(new ListItem( ResourceManager.GetString("LoginSmall_AutoLogin02") ));  
  Items.Add(new ListItem( ResourceManager.GetString("LoginSmall_AutoLogin03")));   
  Items.Add(new ListItem( ResourceManager.GetString("LoginSmall_AutoLogin04")));            }}
 

 

虽然也可以直接在皮肤中增加,但这样的话会失去皮肤语言无关性。所以还是要新增加一个类。

 

第三部分:皮肤的修改
我们以Skin-LoginSmall.ascx为例
在引用处增加
 
<%@ Register TagPrefix="Forums" Namespace="AspNetForums.Controls" Assembly="AspNetForums.Controls" %>
 


把以前的


 
<asp:CheckBox id="autoLogin" TextAlign="Left" type="checkbox" Checked="true" runat="server" />
 


替换成


 
<Forums:CookieDropDownList id="autoLogin" runat="server" />
 

 

第四部:自定义控件的修改
登陆都使用的是Login();
Login()类中把所有的CheckBox类型控件修改为DropDownList类型
包括InitializeSkin(Control skin)中的


 
autoLogin = (CheckBox) skin.FindControl("autoLogin");

autoLogin.Text = ResourceManager.GetString("LoginSmall_AutoLogin");
 


替换为


 
autoLogin = (DropDownList) skin.FindControl("autoLogin");
 


增加一个函数用来写入Cookie


 
public void set_Cookie(string Username,int Selet_item){   switch(Selet_item)     {

case 0:

FormsAuthentication.SetAuthCookie(Username,false); break;

case 1:

FormsAuthentication.SetAuthCookie(Username,true);

forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddDays(1); break;

case 2:

 FormsAuthentication.SetAuthCookie(Username,true);

forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddMonths(1); break;

case 3:

FormsAuthentication.SetAuthCookie(Username,true);

forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddYears(1); break; }}
 


在LoginButton_Click(Object sender, EventArgs e)中将
 
FormsAuthentication.SetAuthCookie(userToLogin.Username, autoLogin.Checked)
 


 替换为
 
set_Cookie(userToLogin.Username,autoLogin.SelectedIndex);
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值