繁体   English   中英

使用C#读取Cookie值

[英]Read cookie value with c#

使用asp.net/c#,怎么可能:

  • 仅在Cookie值设置为“ 1”时才显示网络表单。 (在page_load事件上)因此,asp代码应仅读取cookie值并使Web窗体可见/不可见

请注意,我将使用php设置cookie值。 请注意,HTML Webform包含在代码中:

asp:内容ID =“ webform” ContentPlaceHolderID =“ webform1” runat =“服务器”

因此,我需要一种方法来根据每次加载页面时都会读取的cookie值的设置来操作此网络表单

如果(!Page.IsPostback)

根据返回值response.redirect调用下面的方法以重定向到另一页。

private string GetCookieValue(string cookieName, string itemName)
{
    var CookieName = "MY_COOKIE";
    var CookieValue = string.empty;

    HttpCookie myCookie = Request.Cookies[CookieName];
    if (myCookie == null) return "No cookie found";

    //If you added a key vs. the value in the cookie use this code
    //CookieValue = myCookie[itemName].ToString();

    //Get the value of the cookie if you are not using a key
    CookieValue = myCookie.Value.ToString();

    Return CookieValue;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM