我正在尝试将框架从 .net 4.8 框架更新到 .net8 后修复代码。我正在处理我的代码中的一个问题:
controller.User = new GenericPrincipal(new GenericIdentity("test"), new[] { Roles.VirtualUser });
controller.User 显然是只读的。这就是 User 在 Microsoft.AspNetCore.mvc 命名空间中的样子:
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> for user associated with the executing action.
/// </summary>
public ClaimsPrincipal User => HttpContext?.User!;
我不确定是什么导致这段代码被破坏。我读过类似的帖子,但我不相信其中任何一个有确切的问题。我已经看到我可能需要一个 setter 方法,但不相信我可以编辑库代码。任何建议/提示将不胜感激。谢谢!
您可以使用
HttpContext
一个。例如在控制器操作中:
HttpContext.User =
new GenericPrincipal(new GenericIdentity("test"), new[] { Roles.VirtualUser });