Created
November 3, 2012 09:37
-
-
Save morsdyce/4006806 to your computer and use it in GitHub Desktop.
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
public class MembershipService : IMembershipService | |
{ | |
private readonly MembershipProvider _provider; | |
public MembershipService() | |
: this(null) | |
{ | |
} | |
public MembershipService(MembershipProvider provider) | |
{ | |
_provider = provider ?? Membership.Provider; | |
} | |
public int MinPasswordLength | |
{ | |
get | |
{ | |
return _provider.MinRequiredPasswordLength; | |
} | |
} | |
public bool ValidateUser(string userName, string password) | |
{ | |
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); | |
return _provider.ValidateUser(userName, password); | |
} | |
public MembershipCreateStatus CreateUser(string userName, string password, string email) | |
{ | |
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); | |
if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); | |
MembershipCreateStatus status; | |
_provider.CreateUser(userName, password, email, null, null, true, null, out status); | |
return status; | |
} | |
public bool ChangePassword(string userName, string oldPassword, string newPassword) | |
{ | |
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); | |
if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); | |
if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); | |
// The underlying ChangePassword() will throw an exception rather | |
// than return false in certain failure scenarios. | |
try | |
{ | |
MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */); | |
return currentUser.ChangePassword(oldPassword, newPassword); | |
} | |
catch (ArgumentException) | |
{ | |
return false; | |
} | |
catch (MembershipPasswordException) | |
{ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment