MVC Custom Membership Provider and AccountController.cs

1.2k views Asked by At

I have implemented my own custom MembershipProvider, configured it within web.config and my initialize method is successfully being called, even though it only contains:

    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);
    }

I have also implemented all the other standard methods like:

    public override bool ChangePassword(string username, string oldPassword, string newPassword)

and

    public override bool ValidateUser(string username, string password)

My understanding of the benefits of bothering to write a custom MembershipProvider was that once you've implemented the interface and initialized it, the appropriate methods would automatically get called at the appropriate times by the standard MVC 5 web site. I.e. when a user logs in or registers etc. But none of my methods are being called, apart from Initialize()...

Am I expected to have to go through the standard AccountController.cs and change all of the methods in there to force it to call my custom MembershipProvider?

E.g. this is the standard Login() method:

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.Email, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Do I have to modify this to make sure its calling my stuff?

Or what am I missing?

Thanks.

1

There are 1 answers

0
AudioBubble On

On your web.config at the bottom of your solution, paste this under <system.web>

//This is just easy to modify

<membership defaultProvider="YourCustomMembershipProvider">
      <providers>
        <clear /> //this clears up all the default membership called on init
        <add name="YourCustomMembershipProvider" passwordFormat="Hashed" type="ProjectName.YourFolder.YourCustomMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>

    <roleManager enabled="true" defaultProvider="YourCustomRoleProvider">
      <providers>
        <clear />
        <add name="YourCustomRoleProvider" type="ProjectName.YourFolder.YourCustomRoleProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" />
      </providers>
    </roleManager>

Hope this helps. :)