【发布时间】:2016-01-17 20:44:28
【问题描述】:
我正在尝试在ActionFilter 的OnActionExecuted 方法中从HttpResponseHeaders 中删除特定的Set-Cookie 标头。
我有几个问题:
- 我看不到枚举标题的方式。收藏总是 空的,即使我在调试器中看到标题。
- 因为我不能
枚举,我无法删除特定的标题。我只能删除所有
具有相同键的标题,但
Set-Cookie可以有多个 条目。
目前我正在删除所有 cookie,但这不是我想要的。
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
HttpResponseHeaders headers = actionExecutedContext.Response.Headers;
IEnumerable<string> values;
if (headers.TryGetValues("Set-Cookie", out values))
{
actionExecutedContext.Response.Headers.Remove("Set-Cookie");
}
base.OnActionExecuted(actionExecutedContext);
}
【问题讨论】:
-
你有解决这个问题的办法吗?
-
@KalpeshBoghara 只能通过解决方法。在过滤器中为特定操作添加上下文项
OnActionExecutingHttpContext.Current.Items.Add("RemoveAuthCookieKey", true);。然后在 Global.asax 中Application_EndRequest如果密钥存在this.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
标签: c# cookies asp.net-web-api asp.net-web-api2