graphql - 如何在 .Net 5 中使用 Hot Chocolate 在 GraphQL 中修改响应 Cookie
问题描述
我正在使用 Hot Chocolate(.net 5) 构建 GraphQL API,并且需要使用 JWT 令牌添加身份验证。
在 REST API 中,我使用了仅 http cookie 来添加刷新令牌。
var cookieOption = new CookieOptions
{
HttpOnly = true,
Expires = DateTime.UtcNow.AddDays(7)
};
Response.Cookies.Append("refreshToken", <refreshToken.Token>, cookieOption);
在我的登录突变中,我无法像在 REST API 中那样访问 HttpResponse。
甚至 Hot Chocolate 的文档也没有关于如何访问 Http 响应的示例或说明。
我非常感谢这方面的任何帮助。
谢谢
解决方案
您可以使用 IHttpContextAccessor 访问 HttpContext 并反过来修改 cookie。
public string Foo(string id, [Service] IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor.HttpContext is not null)
{
httpContextAccessor.HttpContext.Response.Cookies...
}
}
https://chillicream.com/docs/hotchocolate/fetching-data/resolvers/#ihttpcontextaccessor
推荐阅读
- android - 如何在Android中以编程方式在分屏模式下切换视图?
- python - 在 Python 中找不到路径
- python - 如何获取 Azure 表查询中返回的实体数?
- javascript - Typescript 可选链接结合三元运算符解析错误
- perl - 转换字符串中字符的位置以考虑“间隙”(即字符串中的非字母数字字符)
- file-upload - 通过 Microsoft Teams Graph Api 将文件上传到频道
- javascript - .play() 在 React 组件上无法按预期工作
- sql - 查找一年内销量最高的 10 种产品
- python - 将列添加到熊猫数据框中,列中的值除以基于另一列的组的列的最大值?
- swiftui - SwiftUI - ForEach 循环中的左挤压视图