繁体   English   中英

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

[英]ASP.NET Combining Windows Authentication with Custom Application Groups/Roles

完全披露,我不完全了解Windows身份验证,活动目录和LDAP的世界,并且通过sql server对个人用户帐户几乎没有经验。 此外,我发现Web上的大多数文档,特别是Microsoft提出的文档,都假设您在纯Microsoft世界中开发,并且能够实现它们提供的最新和任何解决方案,框架或服务。

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

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

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

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

2)基于用户角色操纵功能

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

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

关于我如何处理这些或所有这些问题的任何信息都将是非常有益的。

您正在寻找的是自定义角色提供程序。 这非常简单易行。 只需创建一个继承自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
}

然后,在Web.Config中

<system.web>
    <roleManager defaultProvider="CustomRoleProvider" enabled="true">
      <providers>
          <clear />
          <add name="CustomRoleProvider" type="ThisProject.CustomRoleProvider, ThisProject" />
      </providers>
    </roleManager>
</system.web>

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

有专门为ActiveDirectory创建的成员资格提供程序:

https://msdn.microsoft.com/en-us/library/system.web.security.activedirectorymembershipprovider(v=vs.110).aspx

您可以在应用中实施该提供商。 此外,如果您需要ActiveDirectoryMembershipProvider未提供的其他功能,您可以创建自己的成员资格提供程序:

https://msdn.microsoft.com/en-us/library/f1kyba5e.aspx

Asp.Net Identity将Identity和Authorization分为两个不同的组件。

通过设计,您可以选择将AD标识片与Asp.Net授权片一起使用。 这样您就可以使用本地AD令牌来识别用户所在的WHO,然后使用该令牌根据该身份为其分配权限(角色和/或声明)。 与您如何使用Google,Facebook或Twitter身份类似。 显然,如果您的AD权限不允许您查询AD“谁是令牌X后面的用户”,那么这个答案没有实际意义。

我现在没有时间进一步研究这个问题,但我认为这应该让你朝着正确的方向前进。

(请注意:您可能只能使用Microsoft浏览器。最后我看起来只有IE会发送带有HttpRequest的Active Directory令牌,如果请求被发送到本地域服务器(也就是“内部网”区域)。我听说过Chrome将允许您配置它也执行此操作,但从未实际执行过此操作。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM