HttpContext.Current.User 和 this.User.Identity 的区别

Car*_*nta 2 c# asp.net-mvc

HttpContext.Current.User.Identity.IsAuthenticated和 和有this.User.Identity.IsAuthenticated什么区别?有时它们出现在我的代码中,有时它们没有(所以我必须使用另一个)。它们的意思是一样的还是应该有一种特定的“上下文”让我使用它们?

我有我在其中一个课程中使用的代码:

if (!HttpContext.Current.User.Identity.IsAuthenticated) {

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

那么什么是thisHttpContext?我知道this指的是一个特定的实例,但是HttpContext呢?

Mis*_*ers 5

HttpContext.Current.User.Identity.IsAuthenticated并且this.User.Identity.IsUthenticated是相同的东西,但用法不同。

this.User.Identity.IsUthenticated在控制器内部使用,但如果您在控制器外部并且您需要知道用户是否经过身份验证,您使用HttpContext.Current.User.Identity.IsAuthenticated.