据我所知,WebSecurity 只是一个包装器。
没错,两者是一样的。让我们看看如何WebSecurity.IsAuthenticated
属性的实现:
public static bool IsAuthenticated
{
get
{
return Request.IsAuthenticated;
}
}
现在让我们看看如何WebSecurity.Request
静态属性的实现:
internal static HttpRequestBase Request
{
get
{
return Context.Request;
}
}
最后让我们看看如何WebSecurity.Context
静态属性的实现:
internal static HttpContextBase Context
{
get
{
return new HttpContextWrapper(HttpContext.Current);
}
}
正如你所看到的:
WebSecurity.IsAuthenticated
是相同的:
new HttpContextWrapper(HttpContext.Current).Request.IsAuthenticated
这又等于Context.User.Identity.IsAuthenticated
略有不同的是,存在空检查,并且该属性将返回 false,例如Identity
属性为空。
我应该使用它还是 User.Identity 有不同的功能?
即使两者严格等效,我也会使用User.Identity
这是官方的 ASP.NET 实现,因为如果明天您决定用其他东西替换简单的成员资格提供程序,您的代码中需要替换的东西就会少得多。