## WebSecurity.GetPasswordChangedDate() Method
The `WebSecurity.GetPasswordChangedDate()` method is a built-in function in ASP.NET Web Pages (SimpleMembership) that returns the date and time when a specific user's password was last changed.
---
## Definition
This method retrieves the timestamp of the most recent password modification for the specified user account. It is highly useful for implementing security audits, enforcing password expiration policies, or displaying account history to users.
---
## Syntax
### C# Syntax
```csharp
public static DateTime GetPasswordChangedDate(string userName)
```
### VB.NET Syntax
```vb
Public Shared Function GetPasswordChangedDate(userName As String) As DateTime
```
---
## Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `userName` | `String` | The unique username of the user whose password change date you want to retrieve. |
---
## Return Value
| Return Type | Description |
| :--- | :--- |
| `DateTime` | The date and time when the user's password was last changed. |
---
## Exceptions and Errors
An `InvalidOperationException` will be thrown if you attempt to call this method under the following conditions:
* The `WebSecurity.InitializeDatabaseConnection()` method has not been called to initialize the membership database.
* **SimpleMembership** is not initialized, or it has been explicitly disabled in the website configuration.
---
## Remarks
* **Default Value:** If the user's password has never been changed since the account was created, the method returns `DateTime.MinValue`.
* **DateTime.MinValue Value:** The value of `DateTime.MinValue` is `00:00:00.0000000, January 1, 0001`. You can use this value in your conditional logic to determine if a password is still in its initial state.
---
## Code Example
The following example demonstrates how to retrieve the last password change date for a logged-in user and display it on an ASP.NET Web Pages (Razor) site.
```cshtml
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Password Security Info";
string message = "";
if (WebSecurity.IsAuthenticated)
{
string currentUserName = WebSecurity.CurrentUserName;
try
{
// Retrieve the last password change date
DateTime lastChanged = WebSecurity.GetPasswordChangedDate(currentUserName);
if (lastChanged == DateTime.MinValue)
{
message = "You have not changed your password since your account was created.";
}
else
{
message = "Your password was last changed on: " + lastChanged.ToString("F");
}
}
catch (InvalidOperationException ex)
{
message = "Security service error: " + ex.Message;
}
}
else
{
message = "Please log in to view your password history.";
}
}
Account Security
Password History
@message
```
---
## Technical Reference
| Property | Value |
| :--- | :--- |
| **Namespace** | `WebMatrix.WebData` |
| **Assembly** | `WebMatrix.WebData.dll` |
| **Applies to** | ASP.NET Web Pages 2, ASP.NET Web Pages 3 |