Current User ID is: @userId
``` ### VB.NET Example (Razor) ```html @Code Dim userId As Integer userId = WebSecurity.CurrentUserId End CodeCurrent User ID is: @userId
``` --- ## Remarks & Considerations * **Read-Only:** The `CurrentUserId` property is strictly read-only. You cannot programmatically assign a value to this property to change the logged-in user. * **Database Mapping:** This ID corresponds directly to the primary key column (typically `UserId`) in your user profile and membership tables configured in the database. * **Anonymous Users:** If no user is currently logged in (i.e., the request is anonymous), the property returns `-1`. ### Errors and Exceptions Any attempt to access the `WebSecurity` object or its properties will throw an `InvalidOperationException` under the following conditions: 1. The `WebSecurity.InitializeDatabaseConnection()` method has not been called (usually initialized in `_AppStart.cshtml`). 2. The **SimpleMembership** provider is not initialized or has been explicitly disabled in the website configuration (`web.config`). --- ## Technical Reference | Attribute | Value | | :--- | :--- | | **Namespace** | `WebMatrix.WebData` | | **Assembly** | `WebMatrix.WebData.dll` | | **Return Type** | `System.Int32` (Integer) |
YouTip