文字
分享

WebSecurity 属性 - CurrentUserId




定义

CurrentUserId 属性是当前用户在 WebSecurity 数据库中的 ID(主键)。


C# 和 VB 语法

1

<div> WebSecurity.CurrentUserId</div>


实例

实例 C#

1

<div class="example_code notranslate"> @{<br> int value;<br> value=WebSecurity.CurrentUserId;<br> }<br><br> <p>Current User ID is: @value</p></div>

实例 VB

1

<div class="example_code notranslate"> @Code<br> Dim value as Integer<br> value=WebSecurity.CurrentUserId<br> End Code<br><br> <p>Current User ID is: @value</p></div>


备注

CurrentUserId 属性是只读的。它不能通过代码更改。

该属性用于识别 WebSecurity 数据库中用户资料表和会员表中的用户。


错误和异常

如果当前没有用户登录,则 CurrentUserId 属性返回 -1。

在下面的情况下,任何对 WebSecurity 对象的访问将抛出一个 InvalidOperationException

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

技术数据

名称
Namespace WebMatrix.WebData
Assembly WebMatrix.WebData.dll