我有以下嘲笑:
var MockHttpContext = new Mock<HttpContextBase>();
var MockPrincipal = new Mock<IPrincipal>();
MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object);
Run Code Online (Sandbox Code Playgroud)
测试此行时发生错误:
var user = (CustomPrincipal)httpContext.User;
Run Code Online (Sandbox Code Playgroud)
这是错误:
Unable to cast object of type 'IPrincipalProxy5c6adb1b163840e192c47295b3c6d696'
to type 'MyProject.Web.CustomPrincipal'.
Run Code Online (Sandbox Code Playgroud)
我的CustomPrincipal实现了IPrincipal接口.那么任何人都可以解释为什么我会得到这个错误以及我如何解决它?
我想知道在httpcontext.current.items而不是httpcontext.current.user中存储自定义Principal是否有任何缺点.我知道你需要为每个请求设置它,但我也必须使用httpcontext.current.user.
提前致谢,
Pickels