YouTip LogoYouTip

Prop Websecurity Currentuserid

## WebSecurity.CurrentUserId Property The `CurrentUserId` property is a member of the `WebSecurity` class, used to retrieve the unique identifier (primary key) of the currently logged-in user from the membership database. This property is part of the ASP.NET Web Pages (Razor) Security API, specifically within the SimpleMembership provider framework. --- ## Definition The `CurrentUserId` property returns an integer representing the unique ID of the currently authenticated user in the `WebSecurity` database. --- ## Syntax This property is read-only and can be accessed in both C# and Visual Basic (VB) syntax within your Razor pages. ### C# Syntax ```csharp WebSecurity.CurrentUserId ``` ### VB Syntax ```vb WebSecurity.CurrentUserId ``` --- ## Code Examples Below are practical examples demonstrating how to retrieve and display the current user's ID in both C# and VB.NET. ### C# Example (Razor) ```html @{ int userId; userId = WebSecurity.CurrentUserId; } Get Current User ID - C#

Current User ID is: @userId

``` ### VB.NET Example (Razor) ```html @Code Dim userId As Integer userId = WebSecurity.CurrentUserId End Code Get Current User ID - VB

Current 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) |
← Prop Websecurity CurrentusernaAtt String Find β†’