日期:2014-05-17  浏览次数:20457 次

使用Forms验证的时候,如果客户端禁用了cookie该怎么办?
花了一天时间了。最后这个问题还是没有解决,网上搜了一把,还是无功而返。。。

算了,请教一下大牛们,先谢啦!

先贴一段代码吧,算是记录一下。

C# code

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        userName = txtUserName.Text.Trim();
        pwd = txtPwd.Text.Trim();

        if (userName == "xzl" && pwd == "123") 
        {
            ////方式一:授权并且自动跳转。
            //FormsAuthentication.RedirectFromLoginPage(userName, false);//将经过身份验证的用户重定向回“最初请求的 URL 或默认 URL”。



            ////方式二:授权,自由控制跳转。
            //FormsAuthentication.SetAuthCookie(userName, false);//发放凭证
            //Response.Redirect("Default.aspx");



            ////方式三(1):自定义授权,自由控制转向。(方式三1中,效果和方式一、二中差不多)
            //创建加密的 Forms 身份验证票证的 FormsAuthenticationTicket 对象。
            //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddHours(1), false, "userDate", FormsAuthentication.FormsCookiePath);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(userName, false, 300);

            //根据“加密的 Forms 身份验证票证的 FormsAuthenticationTicket 对象”生成一个字符串,该字符串存放客服端cookie中。
            string encTicket = FormsAuthentication.Encrypt(ticket);

            string cookieAuthKey = FormsAuthentication.FormsCookieName;//获取cookie中的key值,该值根据配置文件<forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"></forms>中的“name”属性确定。

            HttpCookie cookie = new HttpCookie(cookieAuthKey, encTicket);

            cookie.Expires = DateTime.Now.AddDays(3);//设置cookie过期时间

            Response.Cookies.Add(cookie);//发放凭证

            string redirectUrlOld = FormsAuthentication.GetRedirectUrl(userName, false);//返回导致重定向到登录页的“原始请求的重定向 URL”。

            Response.Redirect(redirectUrlOld);//重定向到原始页面



            //“登录页”根据配置文件<forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"></forms>中的loginUrl确定。
            //string loginUrl = FormsAuthentication.LoginUrl;//获取“登录页”。
            //FormsAuthentication.RedirectToLoginPage();//定向到“登录页”。


            ////方式三(2):
            //HttpCookie coo = FormsAuthentication.GetAuthCookie(userName,false);
            //Response.Cookies.Add(coo);
            //Response.Redirect("Default.aspx");
            return;
        }
        Response.Write("用户名或密码错误!");
    }




问题来了:
1、使用Forms验证的时候,如果客户端禁用了cookie该怎么办?
2、服务器端的验证票存储在什么“地方”,就像sessionid一样,有好几种存储方式,那么存储的这个“验证票证”会不会不稳定呢?

------解决方案--------------------
1,禁用cookie你可以进行提示,或者采用<sessionState cookieless="true" />这样的方法存储到url里面。

2,服务器端不进行保存的,都是cookie传递的,然后服务器端解密
------解决方案--------------------
会将身份验证标记保留在 Cookie 或页的 URL 中。
当web.config配置文件中 Mode 设置为 Forms的时候,会经过FormsAuthenticationModule,FormsAuthenticationModule 继承IHttpModule.
会获取cookie并解密重新获取到验证票,然后设置用户标识HttpContext.User
------解决方案--------------------
<authentication mode="Windows">
<forms 
name=".ASPXAUTH" 
loginUrl="login.aspx" 
defaultUrl="default.aspx" 
protection="All" 
timeout="30" 
path="/" 
requireSSL="false" 
slidingExpiration="true" 
cookieless="UseDeviceProfile" domain="" 研究一下这个
enableCrossAppRedirects="false">
<credentials passwordFormat="SHA1" />
</forms>
<passport redirectUrl="internal" />
</authentication>