asp.net api 返回cookie 操作

< ![CDATA[

1.web api 返回cookie有很多作用.比如标识身份.存储信息,或者其他的.or加密数据

 

 public HttpResponseMessage Get()
        {
            HttpResponseMessage respMessage = new HttpResponseMessage();
            respMessage.Content = new ObjectContent<string[]>(new string[] { "value1", "value2" }, new JsonMediaTypeFormatter());

            var nv = new NameValueCollection();
            nv["sessid"] = "1234";
            nv["3dstyle"] = "flat";
            nv["theme"] = "red";
            var cookie = new CookieHeaderValue("session", nv);

            cookie.Expires = DateTimeOffset.Now.AddDays(1);
            cookie.Domain = Request.RequestUri.Host;
            cookie.Path = "/";
            respMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });
            return respMessage;
        }
   // POST api/values
        public void Post([FromBody]string value)
        {
            string sessionId = "";
            string style = "";
            string theme = "";

            CookieHeaderValue cookie = Request.Headers.GetCookies("session").FirstOrDefault();
            if (cookie != null)
            {
                CookieState cookieState = cookie["session"];

                sessionId = cookieState["sessid"];
                style = cookieState["3dstyle"];
                theme = cookieState["theme"];
            }
        }

  或者想全局设置cookie返回.

就Handlers  里面搞吧,集成DelegatingHandler

因为当url--进去collection 会用SendAsync 这个方法.那就重写这个方法

    static public string CookieStampToken = "cookie-stamp";
        protected async override Task<HttpResponseMessage> SendAsync(
         HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            string cookie_stamp;
            var cookie = request.Headers.GetCookies(CookieStampToken).FirstOrDefault();
            if (cookie == null)
            {
                cookie_stamp = "COOKIE_STAMPER_" + Guid.NewGuid().ToString();
            }
            else
            {
                cookie_stamp = cookie[CookieStampToken].Value;
                try
                {
                    Guid guid = Guid.Parse(cookie_stamp.Substring(22));
                }
                catch (FormatException)
                {
                    // Invalid Stamp! Create a new one.
                    cookie_stamp = "COOKIE_STAMPER_" + Guid.NewGuid().ToString();
                }
            }
            // Store the session ID in the request property bag.
            request.Properties[CookieStampToken] = cookie_stamp;
            // Continue processing the HTTP request.
            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
            // Set the session ID as a cookie in the response message.
            response.Headers.AddCookies(new CookieHeaderValue[] {
               new CookieHeaderValue(CookieStampToken, cookie_stamp) 
              });
            return response;
        }

 

这时候.访问url的时候,会自动设置cookie

 

 

]]>

你可能感兴趣的:(asp.net)