Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom membership or not

I am creating website (football, soccer) in ASP.NET MVC3 and I want have users (with additional information then user in default membership, these are ordinary visitors) and players which I think it is best thet they would inherit users and have some addional iformation as dress number, ... Players also could post articles, users can just comment articles. What is best way to do this? Should I use default membership provider or should I make my own or use some 3rd party solutions? And can you post some articles and tutorials for changing original provider or article for making own provider for asp.net MVC3? Or is it same as MVC2?

like image 312
Libor Zapletal Avatar asked Mar 31 '11 07:03

Libor Zapletal


2 Answers

It is very easy to create your own Membership Provider. Just create class derived from MembershipProvider. And implement members which look into DB, for example (or any other data source).

public class YourMembershipClass: MembershipProvider
{
    public override bool ValidateUser(string username, string password)
    {
         return YourDataLayer.ValidateUser(username, password);
    }
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        return YourDataLayer.GetSpecificUser(providerUserKey, userIsOnline);
    }
    // Implement the other methods as well
 }

Then add your YourMembershipClass to web.config:

<membership defaultProvider="MlgMembership">
  <providers>
    <clear />
    <add name="CustomMembership" type="YourMembershipClass" enablePasswordRetrieval="false" />       
  </providers>
</membership>
like image 144
wassertim Avatar answered Oct 21 '22 15:10

wassertim


If you are looking to store profile type information e.g. first name, last name, job title etc. against each user then you should be able to use the Profile system built into ASP.NET Membership. If you are looking to store more identity related information then yes you will have to create some sort of custom membership provider. There is a good video on creating a custom provider on the ASP.NET website: http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider

Regarding allowing different types of users to perform different actions you can use the Roles system built into ASP.NET Membership. You can tell your action methods to only allow calls from users in certain roles. For example if you had a PostArticle action method and you only wanted players to be able to access it you would have something like this:

[Authorize(Roles="Player")]
public ActionResult PostArtcile(){
    return View();
}

The Authorize attribute tells MVC to only allow authenticated users in the "Player" role to call the action method. You'll still need to restrict the availability of a post article link in your front end but there are several ways to do that.

There is a great series of articles by Scott Mitchell which covers all things membership based: http://www.4guysfromrolla.com/articles/120705-1.aspx

like image 35
AndyM Avatar answered Oct 21 '22 17:10

AndyM