ASP.NET MVC 4中的自定义成员身份和角色提供程序

时间:2013-02-14 10:03:00

标签: asp.net asp.net-mvc asp.net-mvc-4

我正在创建一个ASP.NET MVC 4 Web应用程序。我搜索了自定义会员资格,但我找不到好的资源或视频讲座。

大多数都是过时的或死链接。请问您能否就如何开始编写会员和角色提供者提供一些资源。

4 个答案:

答案 0 :(得分:1)

了解会员资格和角色对我来说也很困难,因为你说在网上找不到很多可靠和详细的内容。我尝试观看几个视频来了解这个主题,但不清楚。但是,来自一个名为Code Project的网站的两篇文章来救援。我正在分享这些链接,您可以在其中看到有关自定义成员资格的分步指南

Link 1
链接1将帮助您使用用户名替换电子邮件以进行登录身份验证,这是开发人员在Microsoft提供的身份模块中最常见的自定义之一。

Link2

第二篇文章将帮助您了解向创建的用户添加和附加角色以及如何将用户注册页面的访问权限限制为仅限管理员。这样在这两篇文章的帮助下,我希望您能理解认证和授权的基础知识。

答案 1 :(得分:1)

我建议使用ASP.Net Identity而不是旧的成员资格.ASP.Net Identity比旧成员更好,更灵活,它还支持使用操作过滤器进行基于角色的身份验证,您可以实现自己的自定义提供程序(如作为角色和用户提供者)。

请参阅下面的链接

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/

答案 2 :(得分:0)

ASP.NET MVC 4 Internet模板添加了一些基于 SimpleMembership 构建的新的非常有用的功能。这些更改添加了一些很棒的功能,例如更简单和可扩展的成员资格API以及对OAuth的支持。但是,新的帐户管理功能需要 SimpleMembership ,并且不会对现有的ASP.NET成员资格提供商起作用

在此处查看ASP.NET身份的资源:

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

答案 3 :(得分:0)

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").