How can I add a custom membership provider in my ASP.NET MVC application?

How do I associate my custom membership provider with my ASP.NET MVC [Authorize ()] attribute? I have introduced several guides for creating a custom membership provider, but all the information I found on how to connect it to the application seems to revolve around regular ASP.NET WebForms applications that seem to be part of the cake.

I will stick out with the amount of “magic” that just happens in ASP.NET MVC, which is great, but I'm used to connecting content to WebForms, so this “it just works” methodology is a bit for me. How do I know when I should do a heavy climb, or should I rely solely on the fact that this happens by magic?

Where can I bind my provider to an MVC application? Do I correctly assume that it is called through the [Authorize ()] attribute as soon as I connect it?

+5
source share
3 answers

I hope I can add additional clarity to the other answers, as they really do not explain what is happening, which will not help you confuse.

First of all, implement your custom provider, which is due to what you have already done, so I will just throw out a small piece of code and will not go into details:

using System.Web.Security;

public class MyCustomMembershipProvider : MembershipProvider
{
    public override bool ValidateUser(string username, string password)
    {
        if (username.Equals("BenAlabaster") && password.Equals("Elephant"))
            return true;

        return false;
    }

    /* Override all the other methods required to extend MembershipProvider */        
}

Then you configure your provider in your web.config to populate the attributes that configure the basic Provider membership:

<membership defaultProvider="MyCustomMembershipProvider">      
    <providers>        
        <clear />        
        <add name="MyCustomMembershipProvider" 
             type="MyNamespace.MyCustomMembershipProvider" 
             enablePasswordRetrieval="false"
             enablePasswordReset="true"          
             requiresQuestionAndAnswer="false"          
             requiresUniqueEmail="true"           
             passwordFormat="Hashed"           
             maxInvalidPasswordAttempts="10"           
             minRequiredPasswordLength="6"           
             minRequiredNonalphanumericCharacters="0"           
             passwordAttemptWindow="10"           
             passwordStrengthRegularExpression=""           
             applicationName="/" />      
    </providers>     
</membership>

, , , -. , WebForms - MVC - , , [Authorize] , , ' , . , web.config . , User:

public class WhateverController : Controller
{
    [Authorize]
    public ActionResult WhateverAction()
    {
        ViewData["LoggedInAs"] = string.Format("You are logged in as {0}.", User.Identity.Name);
        Return View();
    }
}

, , Whatever/WhateverAction.aspx, .

+6

web.config:

 <membership defaultProvider="MyMembershipProvider">
  <providers>
    <clear/>
    <add name="MyMembershipProvider" type="Namespace.MyMembershipProvider, Namespace" connectionStringName="connstring" [...] />
  </providers>
</membership>

Membership.

+1

? ? ? ?

, - , Authroize.

However, if your user membership does not work, you probably haven’t configured it correctly, and the setup is the same as in web forms. You just need to configure your webconfig.

  </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ConnectionString"
         enablePasswordRetrieval="false"
         enablePasswordReset="true"
          requiresQuestionAndAnswer="false"
          requiresUniqueEmail="true"
           passwordFormat="Hashed"
           maxInvalidPasswordAttempts="10"
           minRequiredPasswordLength="6"
           minRequiredNonalphanumericCharacters="0"
           passwordAttemptWindow="10"
           passwordStrengthRegularExpression=""
           applicationName="/"  />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ConnectionString" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="true">
      <providers>
        <clear />
        <add connectionStringName="ConnectionString"
          applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider"
          type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </providers>
    </roleManager>
0
source

All Articles