[英]What is the difference between HttpContext.Current.User and HttpContext.User
[英]Difference between HttpContext.Current.User and this.User.Identity
HttpContext.Current.User.Identity.IsAuthenticated
和this.User.Identity.IsAuthenticated
什么区别? 有时它们出现在我的代码中,有时它们没有(所以我必须使用另一个)。 它们的意思是一样的还是应该有一种特定的“上下文”让我使用它们?
我有我在其中一个课程中使用的代码:
if (!HttpContext.Current.User.Identity.IsAuthenticated) {
return 0;
}
那么,什么是this
和HttpContext
? 我知道this
是指一个特定的实例,但是HttpContext
呢?
HttpContext.Current.User.Identity.IsAuthenticated
和this.User.Identity.IsUthenticated
是一回事,但用法不同。
您在控制器内部使用this.User.Identity.IsUthenticated
但如果您在控制器外部并且您需要知道用户是否通过身份验证,则使用HttpContext.Current.User.Identity.IsAuthenticated
。
完全没有区别。 HttpContext.Current.User 和 Controller.User 都是一样的。
“this”是您的控制器,而 BaseController 具有用户属性。
事情是这样的。
public class Controller
{
public User { get { return HttpContext.Current.User; } }
}
public class YourController : Controller
{
public ActionResult Index()
{
return this.User.Identity.Name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.