![](/img/trans.png)
[英]How can I use ASP.NET to check if cookies are enabled without having a web page?
[英]How to check if cookies are enabled or disabled in asp.net?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Browser.Cookies)
{
if (Request.QueryString["check"] == null)
{
HttpCookie cookie = new HttpCookie("testcookie");
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx?check=1");
}
else
{
HttpCookie cookie = Request.Cookies["testcookie"];
if(cookie==null)
{
Label1.Text = "enable cookies";
}
}
}
else
{
Label1.Text = "cookies not supported:";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("userinfo");
cookie["name"] = TextBox1.Text;
cookie["email"] = TextBox2.Text;
//cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
Response.Redirect("Default2.aspx");
}
它無法正常工作。
請參閱以下鏈接。
http://forums.asp.net/t/1044823.aspx?How+to+check+cookies+enabled+in+a+web+browser+
唯一的檢查方法是設置一個cookie然后重定向它,然后再次檢查你是否能夠訪問它。 所以嘗試以上鏈接中提到的方法。
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsCookieDisabled())
errorMsgLabel.Text = Resources.Resource.BrowserDontSupportCookies;
}
private bool IsCookieDisabled()
{
string currentUrl = Request.RawUrl;
if (Request.QueryString["cookieCheck"] == null)
{
try
{
HttpCookie c = new HttpCookie("SupportCookies", "true");
Response.Cookies.Add(c);
if (currentUrl.IndexOf("?") > 0)
currentUrl = currentUrl + "&cookieCheck=true";
else
currentUrl = currentUrl + "?cookieCheck=true";
Response.Redirect(currentUrl);
}
catch(Exception ex)
{
return false;
}
}
if (!Request.Browser.Cookies || Request.Cookies["SupportCookies"] == null)
return true;
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.