zoukankan      html  css  js  c++  java
  • C# 检查客户端Cookie是否启用

    发现 System.Web.HttpBrowserCapabilities 类有个属性 Cookies,以为这个属性是侦查目标浏览器是否启用了Cookie的,结果一试才知道根本不是那么回事。上网搜索了一下,发现犯这个错误的人竟不在少数。无奈,既然没有现成的方法可用,那就自己写吧。思路很简单,试图写入一个Cookie,如果不成功就认为客户端禁用了Cookie。代码很简单,如下:

    public partial class Login : System.Web.UI.Page {

      
    private const string COOKIE_TEST_KEY = "tce_84kfi50c";

      
    protected void Page_Load(object sender, EventArgs e) {
        
    if (!IsPostBack) { 
          
    // 写入一个Cookie,以测试浏览器是否支持
          Utility.WebUtility.WriteToCookie(COOKIE_TEST_KEY, "True");
        }
      }

      
    protected void btnLogin_Click(object sender, EventArgs e) {
        
    // 用户单击“登录”按钮时检查Cookie是否可用
        if (string.IsNullOrEmpty(Utility.WebUtility.GetCookie(COOKIE_TEST_KEY))) {
          
    // 已确定Cookie被禁用,跳转到通知页面
          Core.Url.Location.RedirectTo(Core.Url.Dialogs.CookieDisabled, null);
        }
      }

    }


  • 相关阅读:
    精选30道Java笔试题解答
    ASM
    Java Decompiler Plugin For Eclipse IDE
    AMQ5540, AMQ5541 and AMQ5542, application did not supply a user ID and password, 2035 MQRC_NOT_AUTHORIZED
    Shell脚本中的export
    Linux set unset命令
    shell中${}的妙用
    ubuntu alsa2
    ubuntu alsa
    计算机启动boot
  • 原文地址:https://www.cnblogs.com/wfyfngu/p/1645039.html
Copyright © 2011-2022 走看看