-3

i'm building a custom membership in .net and i don't understand real benefits of them, what the benefits? Here is my class that inherits from MembershipProvider:

public class MembershipService : MembershipProvider
{
  public override bool ValidateUser(string username, string password)
  {
    Credencial credencial = context.Credencial.Where(x => x.Usuario == username).FirstOrDefault();

    if (credencial == null) return false;

    if (credencial.Senha == password)
    {
      return true;
    }

    return false;
  }
}

And that's my configuration in web.config:

<membership defaultProvider="MembershipService" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add
      name="MembershipService"
      type="Questiona2011.Services.MembershipService" />
  </providers>
</membership>

Here's where i use them:

[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(indexViewModel.Usuario, indexViewModel.Senha))
        {
            FormsAuthenticationService.SignIn(indexViewModel.Usuario, false);
            return RedirectToAction("Index", "Default");
        }
    }

    return View();
} 

I don't understand, this class MembershipService i don't need to inherits from MembershipProvider.

What's the real benefits using a Custom Membership that inherits from MembershipProvider?

Why I need to inherit from MembershipProvider?

Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
  • I don't understand your question. In the code your `MembershipService` clearly inherits from `MembershipProvider`. Are you asking why the inheritance doesn't have to be pointed out in the web.config? – Anders Abel Jul 21 '11 at 17:47
  • 1
    see http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx – Kevin Burton Jul 21 '11 at 17:50
  • @Anders Abel I'm asking Why I need to inherit from MembershipProvider? – Acaz Souza Jul 21 '11 at 17:51
  • You don't have to inherit from MembershipProvider if you are going to roll your own membership completely apart from .NET Membership. But keep one thing in mind, the .NET Membership Provider classes are probably going to offer you a lot more protection than anything you roll on your own unless you are going to put A LOT of time into it. The .NET Membership Provider libraries have had a lot of time, effort and testing put into them. You are probably better off going with the .NET Libraries than trying to roll your own membership. – Scott Lance Jul 21 '11 at 18:29
  • 1
    @Scott-Lance Can you say some sample about the protection, security (authorization and authentication) that custom membership offer to me? – Acaz Souza Jul 21 '11 at 18:33
  • Here is a primer for .NET Membership: http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx Here is a older but more succinct explanation of the SqlMembershipProvider (under .NET v2.0) http://www.4guysfromrolla.com/articles/120705-1.aspx – Scott Lance Jul 21 '11 at 18:40
  • @Acaz, its unclear from your question, but do you realize that you do not have to write a custom membership provider to use Membership? I've used Membership with many project including legacy code and I seldom have to write a custom membership provider - the ones out of the box are great. – saille Jul 24 '11 at 07:27

3 Answers3

3

If you want to know why you must inherit from the framework's base class; perhaps you might check the documentation. Specifically, it says that you must inherit the abstract class MembershipProvider which already inherits ProviderBase. Therefore, as with all abstract class, it must therefore provide some implementation for common functionality (most likely to do with configuration into the config files).

The main benefit for rolling your own is to alter the behaviour of the framework's implementations (for example, to connect to your existing database schema). Doing so enables you to make use of the Membership controls - including Login, LoginView, CreateUserWizard, ChangePassword, etc.

Reddog
  • 15,219
  • 3
  • 51
  • 63
1

If you are already creating custom memebership you might be knowing why you are doing it .

If you need additional fields in your registration you will build custom one . Generally when you create a custom membership provider that uses custom SQL Server tables separate from the pre-defined tables used by the standard provider.

Pit Digger
  • 9,618
  • 23
  • 78
  • 122
  • Yes, i'm using custom Sql Server tables. But Why I need to inherit from MembershipProvider? – Acaz Souza Jul 21 '11 at 17:52
  • You inherit something cause you want to use existing things in membership provider and you are attaching your changes to existing things . If you are going to use your own forms and tables and nothing from membership you don't inherit from membership . – Pit Digger Jul 21 '11 at 18:01
  • And what membership offer to me? – Acaz Souza Jul 21 '11 at 18:04
1

For a time I used it and one day woke up.. what the gain with this inheritance?

The answer is that I got in my scenarios had no gain. From this day forward I no longer use the Membership, I created a table structure and classes I found most appropriate to my reality. And I do not regret.

Overall Membership is indicated to avoid having to reinvent the wheel... the guys still argues that it is stable and has worked years. Any solution that does not bring out this same guarantee. E as the great majority come from webforms using Membership, continued using the same in the MVC.

MY opinion is that you do not win anything with this inheritance. In real it's like you would use a given interface, since it needs implement all the code! Unless using with Web Forms Controls with the Login Controls where i no need to code if I had a Membership Class, but MVC is my scenario where I have no controls.

Anyway, I have to say is this. I've never had a situation that use custom membership (with inheritance) would be better! FormsAuthentication class, RoleProvider class and my custom Authentication class do the work with authentication and authorization.

Acaz Souza
  • 8,311
  • 11
  • 54
  • 97
  • Really? How do you store users passwords? What about URL based authorization using in web.config? What about the Website Admin Tool (WAT)? I like it that someone else has written all that code, and tested it thoroughly for me. And I can get std SQL Membership provider up and running in about 2 minutes flat, including Roles and Profile, and a schema that works perfectly well. – saille Jul 24 '11 at 07:31
  • 1
    @saille My question was about the Custom MembershipProvider, not the SqlMembershipProvider. URL based authorization using is FormsAuthentication, I use it normally without Membership. – Acaz Souza Jul 25 '11 at 12:45