且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能创建一个登录系统的ASP.NET MVC,但不使用的MembershipProvider?

更新时间:2023-01-06 20:23:38

我有这种相同的要求。我有我自己的用户和角色架构和不愿迁移到asp.net会员架构,但我也想使用ASP.NET MVC操作筛选检查的授权和角色。我不得不这样做挖搞清楚需要做了相当多的,但最终它是比较容易的。我会救你的麻烦,告诉你我做了什么。

I had this exact same requirement. I had my own user and role schema and did not want to migrate to the asp.net membership schema but I did want to use the ASP.NET MVC action filters for checking authorization and roles. I had to do a fair amount of digging to find out exactly what needed to be done, but in the end it was relatively easy. I'll save you the trouble and tell you what I did.

1)我创建了一个从System.Web.Security.MembershipProvider派生的类。的MembershipProvider有一吨,为各种认证相关的功能的抽象方法,如忘记密码,修改密码,创建新的用户,等等。所有我想要的是对我自己的模式进行身份验证的能力。所以我的课主要包含空覆盖。我刚刚推翻的ValidateUser:

1) I created a class that derived from System.Web.Security.MembershipProvider. MembershipProvider has a ton of abstract methods for all sorts of authentication-related functions like forgot password, change password, create new user, etc. All I wanted was the ability to authenticate against my own schema. So my class contained mainly empty overrides. I just overrode ValidateUser:

public override bool ValidateUser(string username, string password)
{
    if (string.IsNullOrWhiteSpace(username) ||
        string.IsNullOrWhiteSpace(password))
      return false;

    string hash = EncryptPassword(password);
    User user = _repository.GetByUserName(username);
    if (user == null) return false;

    return user.Password == hash;
}

2)我创建了一个从System.Web.Security.RoleProvider派生的类。再次,我只是为所有的绒毛,我并不需要像创建和更改角色的空实现。我只是推翻了两种方法:

2) I created a class that derived from System.Web.Security.RoleProvider. Again, I just had empty implementations for all the fluff I did not need like creating and changing roles. I just overrode two methods:

public override string[] GetRolesForUser(string username)
{
    User user = _repository.GetByUserName(username);
    string[] roles = new string[user.Role.Rights.Count + 1];
    roles[0] = user.Role.Description;
    int idx = 0;
    foreach (Right right in user.Role.Rights)
        roles[++idx] = right.Description;
    return roles;
}

public override bool IsUserInRole(string username, string roleName)
{
    User user = _repository.GetByUserName(username);
    if(user!=null)
        return user.IsInRole(roleName);
    else
        return false;
}

3)然后我插上这两个类到我的web.config:

3) Then I plugged these two classes into my web.config:

<membership defaultProvider="FirstlookMemberProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear/>
    <add name="FirstlookMemberProvider" type="FirstlookAdmin.DomainEntities.FirstlookMemberProvider, FirstlookAdmin" />
  </providers>
</membership>
<roleManager defaultProvider="FirstlookRoleProvider" enabled="true" cacheRolesInCookie="true">
  <providers>
    <clear/>
    <add name="FirstlookRoleProvider" type="FirstlookAdmin.DomainEntities.FirstlookRoleProvider, FirstlookAdmin" />
  </providers>
</roleManager>

就是这样。默认的授权行动过滤器将使用这些类。您仍然必须处理在登录页面的标志和签署。只需使用标准的表单验证类这就像你通常会。

That's it. The default authorization action filters will use these classes. You will still have to handle the login page sign in and sign off. Just use the standard forms authentication classes for this like you normally would.