ASP.NET 将 Windows 身份验证与自定义应用程序组/角色相结合

LCa*_*way 3 c# asp.net authentication asp.net-mvc active-directory

坦白说,我并不完全了解 Windows 身份验证、Active Directory 和 LDAP 的世界,并且对通过 SQL Server 处理个人用户帐户的经验也很少。此外,我发现网络上的大多数文档,尤其是 Microsoft 提供的文档,都假设您在纯粹的 Microsoft 世界中进行开发,并且有能力实现他们提供的最新解决方案、框架或服务。

我正在开发一个内联网应用程序。由于各种原因,我无法利用 Active Directory 组/角色,但希望尽可能地模仿此功能。因此,我需要能够在应用程序内部管理用户/角色/组。但是,我希望能够在此过程中检测用户的 Windows 身份验证凭据。换句话说,我不希望用户必须注册,也不希望他们必须登录,而是使用他们登录时使用的 Windows 帐户。

然后,应用程序管理的角色将确定用户在应用程序中将具有的各种功能。

这是一个 asp.net MVC 应用程序。我最终需要满足以下有关授权的要求。

1) 将当前 Windows 用户与应用程序用户存储和角色进行比较。可以使用SQL Server这个。

2)根据用户角色操纵功能

3) 允许管理员搜索 AD 并将域\用户添加到商店以及分配组

4)创建Groups并注册到应用程序组件

任何有关我如何解决或所有这些问题的信息都将非常有益。

Kur*_*ous 5

您正在寻找的是自定义角色提供者。做起来非常容易和简单。只需创建一个继承自 System.Web.Security.RoleProvider 的类即可。您需要实现的唯一方法是 IsUserInRole 和 GetRolesForUser。您可以对所有其他方法抛出 NotImplementedException。然后,通过在 System.Web 下设置 roleManager 元素,将其与 Web.Config 中的应用程序绑定。

public class CustomRoleProvider : RoleProvider
{
    private mydatabase db;

    public override string ApplicationName { get; set; }

    public CustomRoleProvider()
    {
        db = new mydatabase();
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        //This will return the user object.
        //To get the username of the logged on user, you can use User.Identity.Name
        //To remove the domain name from the username: User.Identity.Name.Split('\\').Last();
        var user = db.CurrentUser();

        return user.Roles != null
            && user.Roles.Count > 0
            && (user.Roles.Exists(x => x.Roles.RoleNm == roleName));
    }

    public override string[] GetRolesForUser(string username)
    {
        var user = db.CurrentUser();

        return user.Roles.Select(x => x.Roles.RoleNm).ToArray();
    }

    #region not implemented

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        throw new NotImplementedException();
    }

    public override string[] GetAllRoles()
    {
        throw new NotImplementedException();
    }

    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override bool RoleExists(string roleName)
    {
        throw new NotImplementedException();
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

然后,在 Web.Config 中

<system.web>
    <roleManager defaultProvider="CustomRoleProvider" enabled="true">
      <providers>
          <clear />
          <add name="CustomRoleProvider" type="ThisProject.CustomRoleProvider, ThisProject" />
      </providers>
    </roleManager>
</system.web>
Run Code Online (Sandbox Code Playgroud)

免责声明:此代码中可能存在拼写错误,但您应该能够获得 gyst