-
-
Save tugberkugurlu/3836733 to your computer and use it in GitHub Desktop.
Membership without the cruft.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Represents a username/password type login | |
/// </summary> | |
public class FabrikLogin | |
{ | |
public const string FabrikLoginProviderId = "fabrik"; | |
/// <summary> | |
/// A unique identifier for the login. | |
/// </summary> | |
public string Id { get; private set; } | |
/// <summary> | |
/// The login username. | |
/// </summary> | |
public string Username { get; private set; } | |
/// <summary> | |
/// The (hashed) login password. | |
/// </summary> | |
public string Password { get; private set; } | |
/// <summary> | |
/// A unique token used for password reset requests. | |
/// </summary> | |
public string PasswordResetToken { get; private set; } | |
/// <summary> | |
/// The point in time that the password reset token expires. | |
/// </summary> | |
public DateTime PasswordResetTokenExpires { get; private set; } | |
/// <summary> | |
/// Initializes a new <see cref="FabrikLogin"/> instance. | |
/// </summary> | |
public FabrikLogin(string username, string password) | |
{ | |
Ensure.Argument.NotNullOrEmpty(username, "username"); | |
Ensure.Argument.NotNullOrEmpty(password, "password"); | |
Username = username; | |
SetPassword(password); | |
} | |
/// <summary> | |
/// Generate a unique password reset token and sets its <paramref name="expiry"/>. | |
/// </summary> | |
/// <param name="expiry">The period of time that the token is valid for.</param> | |
public string GeneratePasswordResetToken(TimeSpan expiry) | |
{ | |
Ensure.Argument.NotNull(expiry, "expiry"); | |
PasswordResetToken = Guid.NewGuid().ToString(); | |
PasswordResetTokenExpires = DateTime.UtcNow.Add(expiry); | |
return PasswordResetToken; | |
} | |
/// <summary> | |
/// Validates the specified <paramref name="password"/>. | |
/// </summary> | |
/// <param name="password">The password to validate.</param> | |
/// <returns>True if the password is valid, otherwise False.</returns> | |
public bool ValidatePassword(string password) | |
{ | |
Ensure.Argument.NotNullOrEmpty(password, "password"); | |
return Crypto.VerifyHashedPassword(Password, password); | |
} | |
/// <summary> | |
/// Resets the login password. | |
/// </summary> | |
/// <param name="resetToken">A valid password reset token.</param> | |
/// <param name="newPassword">The new password to set.</param> | |
/// <returns>True if the password was reset, otherwise False.</returns> | |
public bool ResetPassword(string resetToken, string newPassword) | |
{ | |
Ensure.Argument.NotNullOrEmpty(resetToken, "resetToken"); | |
Ensure.Argument.NotNullOrEmpty(newPassword, "newPassword"); | |
if (PasswordResetToken.IsNotNullOrEmpty() | |
&& PasswordResetToken.Equals(resetToken) | |
&& PasswordResetTokenExpires >= DateTime.UtcNow) | |
{ | |
SetPassword(newPassword); | |
return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Hashes then sets the password | |
/// </summary> | |
/// <param name="password">The new password to set.</param> | |
private void SetPassword(string password) | |
{ | |
Ensure.Argument.NotNullOrEmpty(password, "password"); | |
Password = Crypto.HashPassword(password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment