Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created November 12, 2014 23:08
Show Gist options
  • Save BryanWilhite/3609936219cbfac0ebcb to your computer and use it in GitHub Desktop.
Save BryanWilhite/3609936219cbfac0ebcb to your computer and use it in GitHub Desktop.
ASP.NET MVC: AccountMembershipProvider
using System.Web.Security;
namespace Fox.Web.Mvc.Infrastructure
{
using Fox.Web.Mvc.Models;
/// <summary>
/// Defines the contract that ASP.NET implements
/// to provide membership services using custom membership providers.
/// </summary>
/// <remarks>
/// For more, see “ASP.Net MVC 3 Custom Membership Provider with Repository Injection”
/// by Dan Harman [http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/]
///
/// Register this provider in web.config:
///
/// <code>
/// &lt;membership defaultProvider="AccountMembershipProvider"&gt;
/// &lt;providers&gt;
/// &lt;clear/&gt;
/// &lt;add name="AccountMembershipProvider"
/// type="Fox.Web.Mvc.Infrastructure.AccountMembershipProvider" /&gt;
/// &lt;/providers&gt;
/// &lt;/membership&gt;
/// &lt;roleManager enabled="true" defaultProvider="AccountRoleProvider"&gt;
/// &lt;providers&gt;
/// &lt;clear/&gt;
/// &lt;add name="AccountRoleProvider"
/// type="Fox.Web.Mvc.Infrastructure.AccountRoleProvider" /&gt;
/// &lt;/providers&gt;
/// &lt;/roleManager&gt;
/// &lt;authentication mode="Forms"&gt;
/// &lt;forms loginUrl="~/Client/LogOn" timeout="2880" /&gt;
/// &lt;/authentication&gt;
/// </code>
///
/// </remarks>
public class AccountMembershipProvider : MembershipProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="AccountMembershipProvider"/> class.
/// </summary>
public AccountMembershipProvider()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AccountMembershipProvider"/> class.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
public AccountMembershipProvider(string applicationName, IAccountRepository repository)
{
this.ApplicationName = applicationName;
this._repository = repository;
}
/// <summary>
/// The name of the application using the custom membership provider.
/// </summary>
/// <returns>The name of the application using the custom membership provider.</returns>
public override string ApplicationName { get; set; }
/// <summary>
/// Updates information about a user in the data source.
/// </summary>
/// <param name="user">A <see cref="T:System.Web.Security.MembershipUser"/> object that represents the user to update and the updated information for the user.</param>
public override void UpdateUser(MembershipUser user)
{
this._repository.UpdateUser(user);
}
/// <summary>
/// Verifies that the specified user name and password exist in the data source.
/// </summary>
/// <param name="username">The name of the user to validate.</param>
/// <param name="password">The password for the specified user.</param>
/// <returns>
/// true if the specified username and password are valid; otherwise, false.
/// </returns>
public override bool ValidateUser(string username, string password)
{
return this._repository.ValidateUser(username, password);
}
IAccountRepository _repository;
#region not implemented:
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new System.NotImplementedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
throw new System.NotImplementedException();
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new System.NotImplementedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new System.NotImplementedException();
}
public override bool EnablePasswordReset
{
get { throw new System.NotImplementedException(); }
}
public override bool EnablePasswordRetrieval
{
get { throw new System.NotImplementedException(); }
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new System.NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new System.NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
throw new System.NotImplementedException();
}
public override int GetNumberOfUsersOnline()
{
throw new System.NotImplementedException();
}
public override string GetPassword(string username, string answer)
{
throw new System.NotImplementedException();
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
throw new System.NotImplementedException();
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new System.NotImplementedException();
}
public override string GetUserNameByEmail(string email)
{
throw new System.NotImplementedException();
}
public override int MaxInvalidPasswordAttempts
{
get { throw new System.NotImplementedException(); }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { throw new System.NotImplementedException(); }
}
public override int MinRequiredPasswordLength
{
get { throw new System.NotImplementedException(); }
}
public override int PasswordAttemptWindow
{
get { throw new System.NotImplementedException(); }
}
public override MembershipPasswordFormat PasswordFormat
{
get { throw new System.NotImplementedException(); }
}
public override string PasswordStrengthRegularExpression
{
get { throw new System.NotImplementedException(); }
}
public override bool RequiresQuestionAndAnswer
{
get { throw new System.NotImplementedException(); }
}
public override bool RequiresUniqueEmail
{
get { throw new System.NotImplementedException(); }
}
public override string ResetPassword(string username, string answer)
{
throw new System.NotImplementedException();
}
public override bool UnlockUser(string userName)
{
throw new System.NotImplementedException();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment