一、FormsAuthentication
解释:当用户登录时,服务器为确认客户端通过验证要通过cookie向客户端写验证(Authenticat)信息,
在登录页面刚验证完成后服务器还没有把cookie 回发到Client,所以会没有值,
当服务器第二次Response的时候,就会从客户端读取Cookie,要想有此Cookie还要在web.config文件中配置相应的参数
/// <summary>
/// 存入Form身份验证票证
/// </summary>
/// <returns></returns>
public static void SetFormAuthenti()
{
//如果为 true,则创建持久 Cookie(跨浏览器会话保存的 Cookie)
FormsAuthentication.SetAuthCookie("存入Form身份验证票证", true);
}
/// <summary>
/// 获得Form身份验证票证
/// </summary>
/// <returns></returns>
public static string GetFormAuthenti()
{
string str = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)//用户是否验证过
{
str = HttpContext.Current.User.Identity.Name;//获取用户名
}
return str;
}
/// <summary>
/// 从浏览器中删除Form身份验证票证
/// </summary>
public static void DelFormAuthenti()
{
FormsAuthentication.SignOut();
}
二、Cookie
//存Cookie
HttpCookie SetCookie = new HttpCookie("ckName");
SetCookie["id"] = "100001271";
Response.