简体   繁体   English

使用C#读取Cookie值

[英]Read cookie value with c#

using asp.net/c#, how is possible to : 使用asp.net/c#,怎么可能:

  • display a webform only when the cookie value has been set to "1". 仅在Cookie值设置为“ 1”时才显示网络表单。 (on page_load event) So the asp code should just read the cookie value and make visible/invisible the webform (在page_load事件上)因此,asp代码应仅读取cookie值并使Web窗体可见/不可见

Note that the cookie value I will set it with php. 请注意,我将使用php设置cookie值。 Note that the html webform is included in the code: 请注意,HTML Webform包含在代码中:

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

So I need a way to manipulate this webform depending of the settings of the cookie value that I will read all the time the page gets loaded 因此,我需要一种方法来根据每次加载页面时都会读取的cookie值的设置来操作此网络表单

if (!Page.IsPostback) 如果(!Page.IsPostback)

Call the method below, based on the return value response.redirect to another page. 根据返回值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