网络安全 - IsAuthenticated 属性


❮ 网络安全

定义

IsAuthenticated 属性是一个布尔值,指示当前用户是否经过身份验证(登录)。

如果当前用户已通过身份验证,则属性值为布尔值true,否则为false


C# 和 VB 语法

WebSecurity.IsAuthenticated

实例

实例 C#

if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("~/Account/Login");
}

实例 VB

if !WebSecurity.IsAuthenticated then
  Response.Redirect("~/Account/Login")
end if

备注

IsAuthenticated 属性是只读的。 无法通过代码更改。


错误和异常

在以下情况下,对 WebSecurity 对象的任何访问都会引发 InvalidOperationException

  • InitializeDatabaseConnection()方法没有被调用
  • SimpleMembership 未初始化(或在网站配置中禁用)

❮ 网络安全