我在做完Rbac(基于角色的访问控制)后, 因为每次都是在本机做的测试, 所以一直没有发现问题(因为运行都是http:localhost/...index.aspx),后来我觉得这个习惯很不好,后来我以后都用IP做为网站的根路径,在vs2005中的工具栏中点 "网站" -- "启动选项" -- "服务器(单选 使用自定义服务器)" ,在 URL 中输入
http://10.12.1.55/dssweb/ (其中的IP是我本机的,dssweb是我网站默认文件夹名) ,这时候以后每次启动程序都是以IP为默认网站,再不是LocalHost了。
我在做完Rbac(基于角色的访问控制)后, 因为每次都是在本机做的测试, 所以一直没有发现问题(因为运行都是http:localhost/...index.aspx),后来我觉得这个习惯很不好,后来我以后都用IP做为网站的根路径,在vs2005中的工具栏中点 "网站" --> "启动选项" --> "服务器(单选 使用自定义服务器)" ,在 URL 中输入
http://10.12.1.55/dssweb/ (其中的IP是我本机的,dssweb是我网站默认文件夹名) ,这时候以后每次启动程序都是以IP为默认网站,再不是LocalHost了。
为了保存登陆用户信息,我采用了Cookies,并设计了一个类,其中保存的信息有用户名,地区列表,角色列表。
一切并不那么顺利,在每次登陆成功进入index.aspx后,发现再进入别的页就发生错误,提示角色列表未有对象实例,我跟程序,发现第二个Cookies值乱码,而第三个Cookies值为空,这到底怎么回事,我跟程序,发现添加三个Cookies值的时候是正确的, name : sa , region : "南京市" , rolelist : "1,2,3,4,5,6"。
可提取的时候为什么错误了呢?
原来IE保存Cookies时候有特定的编码方式,一开始我还以为它把每个Cookies按对象的方式保存,原来它把Cookies值按顺序保存在头里,所以三个Cookis保存在一起,因为有中文,编码发生误认,导致错误。
弄了我2个小时才查出来,晕,就这点小时,后来我把类重新改了,加了编码和解码,一切OK了,贡献给大家,虽然很简单,希望大家碰到类似问题,少走弯路。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// LogInOut 的摘要说明
/// </summary>
public class LogInOut
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public LogInOut()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登陆后保存正确的用户信息
/// </summary>
/// <param name="Name">用户名</param>
/// <param name="RegionList">地区列表</param>
/// <param name="RoleList">角色列表</param>
/// <param name="p">Page页</param>
public static void SaveUserInfomation(string Name, string RegionList, string RoleList, Page p)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p.Response.Cookies.Clear();
HttpCookie cookieName = new HttpCookie("LoginName", System.Web.HttpUtility.UrlEncode(Name, System.Text.Encoding.GetEncoding("GB2312")));
p.Response.Cookies.Add(cookieName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HttpCookie cookieRegion = new HttpCookie("LoginRegion", System.Web.HttpUtility.UrlEncode(RegionList, System.Text.Encoding.GetEncoding("GB2312")));
p.Response.Cookies.Add(cookieRegion);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HttpCookie cookieRole = new HttpCookie("LoginRole", System.Web.HttpUtility.UrlEncode(RoleList, System.Text.Encoding.GetEncoding("GB2312")));
p.Response.Cookies.Add(cookieRole);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 是否成功登陆
/// </summary>
/// <param name="p">Page页</param>
/// <returns>true:成功登录;false:登录失败</returns>
public static bool IsLogined(Page p)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (p.Request.Cookies["LoginName"] == null)
return false;
else if (p.Request.Cookies["LoginName"].Value == string.Empty)
return false;
else
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取登陆后的用户名
/// </summary>
/// <param name="p">Page页</param>
/// <returns>返回用户名</returns>
public static string GetLoginedName(Page p)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (IsLogined(p))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return System.Web.HttpUtility.UrlDecode(p.Request.Cookies["LoginName"].Value, System.Text.Encoding.GetEncoding("GB2312"));
}
else
return "您尚未登陆";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string[] GetLoginedInfo(HttpRequest hr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (hr.Cookies["LoginName"] == null || hr.Cookies["LoginName"].Value == string.Empty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] strInfo = new string[3];
strInfo[0] = System.Web.HttpUtility.UrlDecode(hr.Cookies["LoginName"].Value, System.Text.Encoding.GetEncoding("GB2312"));
strInfo[1] = System.Web.HttpUtility.UrlDecode(hr.Cookies["LoginRegion"].Value, System.Text.Encoding.GetEncoding("GB2312"));
strInfo[2] = System.Web.HttpUtility.UrlDecode(hr.Cookies["LoginRole"].Value, System.Text.Encoding.GetEncoding("GB2312"));
return strInfo;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 注销用户登陆
/// </summary>
/// <param name="hr"></param>
public static void LoginOut(HttpResponse hr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (hr.Cookies["LoginName"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hr.Cookies.Clear();
hr.Cookies["LoginName"].Value = null;
hr.Cookies["LoginRegion"].Value = null;
hr.Cookies["LoginRole"].Value = null;
hr.Cookies["LoginName"].Expires = DateTime.Now.AddDays(-1);
hr.Cookies["LoginRegion"].Expires = DateTime.Now.AddDays(-1);
hr.Cookies["LoginRole"].Expires = DateTime.Now.AddDays(-1);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)