从ActionFilterAttribute上下文中获取Cookie

时间:2017-12-20 10:15:58

标签: asp.net-web-api cookies

如何从ActionFilterAttribute上下文中获取cookie值?

actionContext.Request.Cookies下没有Cookie。 并且无法在Request对象下找到任何Cookie。

using System;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace MapManager.Helpers
{
    public class CheckCsrfHeaderAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.Request.Method == System.Net.Http.HttpMethod.Post 
                || actionContext.Request.Method == System.Net.Http.HttpMethod.Put 
                || actionContext.Request.Method == System.Net.Http.HttpMethod.Delete)
            {
               //GetCookies does not exist
               var cookie = actionContext.Request.Headers.GetCookies("XSRF-TOKEN").FirstOrDefault();

1 个答案:

答案 0 :(得分:0)

HttpRequestHeadersExtensions.GetCookies位于System.Net.Http中, 所以必须包括System.Net.Http