18

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

它们中的大多数要么是过时的要么是死链接。请您推荐一些有关如何开始编写会员和角色提供者的资源。

4

4 回答 4

1

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

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

链接2

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

于 2017-01-04T18:55:17.943 回答
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/

于 2017-09-08T15:42:39.810 回答
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").
于 2019-01-25T08:09:34.570 回答
0

ASP.NET MVC 4 Internet 模板添加了一些新的、非常有用的功能,这些功能建立在SimpleMembership之上。这些更改添加了一些很棒的功能,例如更简单且可扩展的会员 API 和对 OAuth 的支持。但是,新的帐户管理功能需要SimpleMembership,并且不适用于现有的 ASP.NET Membership Providers

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

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

于 2016-09-19T14:47:45.737 回答