Custom Membership and Role provider in ASP.NET MVC 4

后端 未结 4 1740
一个人的身影
一个人的身影 2020-12-05 07:43

I am creating an ASP.NET MVC 4 web application. I googled about custom membership, but I couldn\'t find good resources or video lectures.

Most of them are either out

相关标签:
4条回答
  • 2020-12-05 08:23
    http://logcorner.com/how-to-configure-custom-membership-and-role-provider-using-asp-net-mvc4/
    
    **for creating a CustomerMemberShipClass** your class must implement System.Web.Security.MembershipProvider abstarct class. and you override the method ValidateUser()
    in this ValidateUser() you have to write your own logic based on which you want authenticate user and return true or false according to it.
    
    Sample ValidateUser method 
      public override bool ValidateUser(string username, string password)
            {
               int count=db.GetAll().Where(x => x.UserEmail == username && x.password == password).Count();
               if (count != 0)
                   return true;
               else
                   return false;
            }
    
    later in web.config file you have add the fallowing under <sytem.web> element
    
    
    <membership defaultProvider="MyMembershipProvider">
          <providers>
            <clear/>
            <add name="MyMembershipProvider" type="Write your class name that is implementing membershipproviderclas"/>
          </providers>
        </membership>
    
    after doing this you can validate user using **MemberShip.Validate(Username,password)** which returns true or false based on ur code in ValidateUser() in CustomMemberShipProvider class and this will also set **[Authorize] attribute**
    
    **for creating a CustomRoleProviderClass** your class must inherit System.Web.Secuirty.RoleProvider and override the appropriate method to get the roles for the user
    
    SAmple method for getting roles for user
    
    
     public override string[] GetRolesForUser(string username)
            {
                string[] str={db.GetAll().Where(x=>x.UserEmail==username).FirstOrDefault().Role};
                return str;
            }
    
    after this you must add the fallowing in web.config file in <system.web> element
    
    <roleManager enabled="true" defaultProvider="MyRoleProvider">
          <providers>
            <clear/>
            <add name="MyRoleProvider" type="BLL.DoctorAppointmentRoleProvider"/>
          </providers>
        </roleManager>
    
     and after this u can check the role of the user using attribute **[Authorize(role="admin"])** and in Razor view you can check using User.IsinROle("A").
    
    0 讨论(0)
  • 2020-12-05 08:30

    Understanding about membership and roles was pretty difficult for me too, as you said there are not many reliable and detailed content you will find on web. I tried watching several videos to Understand about this topic but wasn't clear. But then two articles from a website called Code Project came for the rescue. I am sharing these Link where you can see a step by step guide about customize membership

    Link 1
    The link 1 will help you to replace an email with username for login authentication this is one of the most common customization the developers need in the microsoft provided Identity Module.

    Link2

    The second article will help you understand adding and attaching roles to the created user and how to limit the access of user registration page to an Admin only. This way with the help of these two articles I hope that you will Understand the Basics of Authentication and Authorization.

    0 讨论(0)
  • 2020-12-05 08:34

    The ASP.NET MVC 4 Internet template adds some new, very useful features which are built on top of SimpleMembership. These changes add some great features, like a much simpler and extensible membership API and support for OAuth. However, the new account management features require SimpleMembership and won't work against existing ASP.NET Membership Providers

    Check out resources for ASP.NET Identity here:

    http://www.asp.net/identity/overview/getting-started/aspnet-identity-recommended-resources

    0 讨论(0)
  • 2020-12-05 08:37

    I suggest using ASP.Net Identity instead of old membership.ASP.Net Identity is a way better and more flexible than old membership, it also supports role-based authentication using action filters and you can implement your own customized providers (such as role and user providers).

    see links below

    https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application

    http://www.c-sharpcorner.com/article/create-identity-in-simple-ways-using-asp-net-mvc-5/

    0 讨论(0)
提交回复
热议问题