我需要在 WebAPI 管道的身份验证步骤中读取/写入 cookie。我为此创建了一个自定义过滤器。
为了遵守自托管概念,访问 cookie 并将其写入客户端的安全方法是什么?里克·斯特拉尔 http://weblog.west-wind.com/posts/2013/Apr/15/WebAPI-Getting-Headers-QueryString-and-Cookie-Values评论说如果我们使用HttpContext.Current.Response.Cookies.Add()
,并且我的应用程序是自托管的,上下文可能/不会存在。
那么我如何使用 cookie 向客户端写入HttpAuthenticationContext
并且仍然是自我托管安全的?
您无法访问authContext.ActionContext.Response
从内部IAuthenticationFilter.AuthenticateAsync()
。好吧,实际上您可以,但只能设置新的响应并缩短管道的其余部分。
我遇到了同样的问题(需要在身份验证成功后设置 cookie)并通过实施解决它IActionFilter
此外IAuthenticationFilter
:
async Task<HttpResponseMessage> IActionFilter.ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
// Process the request pipeline and get the response (this causes the action to be executed)
HttpResponseMessage response = await continuation();
// Here you get access to:
// - The request (actionContext.Request)
// - The response (response) and its cookies (response.Headers.AddCookies())
// - The principal (actionContext.ControllerContext.RequestContext.Principal)
return response;
}
See: 从 Web Api 2 IAuthenticationFilter AuthenticateAsync 方法设置 cookie https://stackoverflow.com/questions/31873720/set-cookie-from-web-api-2-iauthenticationfilter-authenticateasync-method/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)