【发布时间】:2016-03-25 16:15:28
【问题描述】:
在 WebApi 中,我可以通过这种方式使用 AspIdentity 获取当前用户
User.Identity.GetUserId<long>();
但是,服务层不知道 AspIdentity。而且我不想将当前用户作为所有方法的参数传递。
换句话说,我可以使用什么模式将当前用户带到我想要的任何地方。
这样做的原因是记录当前用户在应用程序中所做的所有活动。
【问题讨论】:
-
它不需要知道
AspIdentity。您可以将从User检索到的IPrincipal向上传递到较低层。否则,唯一剩下的就是使用静态HttpContext,这将是一种反模式。 -
@Nkosi 没有通用的方法吗?喜欢使用这个 System.Security.Claims.ClaimsPrincipal.Current?我正在研究的相关问题stackoverflow.com/questions/53092899/…
标签: asp.net-mvc asp.net-web-api2 asp.net-identity n-tier-architecture