修改cookie

来源:互联网 发布:中国和巴基斯坦 知乎 编辑:程序博客网 时间:2024/05/21 22:28

修改目标:以前登陆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);
 

 

原创粉丝点击