我使用带有来自HotChocolate.AspNetCore.Authorization的[Authorize]属性的HotChocolate实现了GraphQL突变,以在我的GraphQL端点上强制授权。
这工作得很好,我只能在我以管理员身份登录后调用变异...
..。但是现在我想检索授权的用户,但是我似乎找不到这样做的方法。
[ExtendObjectType(Name = "Mutation")]
[Authorize(Roles = new[] { "Administrators" })]
public class MyMutations
{
public bool SomeMethod()
{
// In a regular Web API controller, you can do User.Identity.Name to fetch the user name of the current user. What is the equivalent in Hot Chocolate?
var userName = "";
return false;
}
}有什么想法吗?
发布于 2021-02-12 03:56:31
HotChocolate使用asp.net核心身份验证机制,因此您可以使用HttpContext获取用户。
[ExtendObjectType(Name = "Mutation")]
[Authorize(Roles = new[] { "Administrators" })]
public class MyMutations
{
public bool SomeMethod([Service] IHttpContextAccessor contextAccessor)
{
var user = contextAccessor.HttpContext.User; // <-> There is your user
// In a regular Web API controller, you can do User.Identity.Name to fetch the user name of the current user. What is the equivalent in Hot Chocolate?
var userName = "";
return false;
}
}https://stackoverflow.com/questions/66160935
复制相似问题