简体   繁体   English

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

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

Full disclosure, I do not fully understand the world of windows auth, active directory and LDAP and have had little experience with individual user accounts via sql server. 完全披露,我不完全了解Windows身份验证,活动目录和LDAP的世界,并且通过sql server对个人用户帐户几乎没有经验。 Additionally, I have found that most documentation on the web, especially those put forth by Microsoft, assume that you develop in a pure Microsoft world and have the ability to implement the most current and any which solution, framework, or service they provide. 此外,我发现Web上的大多数文档,特别是Microsoft提出的文档,都假设您在纯Microsoft世界中开发,并且能够实现它们提供的最新和任何解决方案,框架或服务。

I am developing an intranet application. 我正在开发一个Intranet应用程序。 And for various reasons I cannot take advantage of Active Directory groups/roles but would like to mimic this functionality as much as possible. 由于各种原因,我无法利用Active Directory组/角色,但希望尽可能多地模仿此功能。 Thus I need to be able to manage users/roles/groups internally within the application. 因此,我需要能够在应用程序内部管理用户/角色/组。 However, I would like the ability to be able to detect a users Windows Auth credentials in the process. 但是,我希望能够在此过程中检测用户的Windows身份验证凭据。 In other words I do not want the user to have to register nor do I want them to have to log in but rather use the windows account they are signed in as. 换句话说,我不希望用户必须注册,也不希望他们必须登录,而是使用他们登录的Windows帐户。

The application managed roles would then determine various functionality the user will have within the application. 然后,应用程序托管角色将确定用户在应用程序中将具有的各种功能。

This is an asp.net MVC application. 这是一个asp.net MVC应用程序。 I will ultimately need to satisfy the following requirements in regards to authorization. 我最终需要满足以下有关授权的要求。

1) Compare current windows user with application user store and roles. 1)将当前windows用户与应用程序用户存储和角色进行比较。 Can use SQL server this. 可以使用SQL server这个。

2) Manipulate functionality based on a users role 2)基于用户角色操纵功能

3) allow for an admin to search AD and add a domain\\User to the store as well as assign groups 3)允许管理员搜索AD并将域\\用户添加到商店以及分配组

4) Create Groups and register with application components 4)创建组并注册应用程序组件

Any information as to how I could address on or all of these would be vastly beneficial. 关于我如何处理这些或所有这些问题的任何信息都将是非常有益的。

What you are looking for is a custom role provider. 您正在寻找的是自定义角色提供程序。 It is extremely easy and simple to do. 这非常简单易行。 Simply create a class that inherits from System.Web.Security.RoleProvider. 只需创建一个继承自System.Web.Security.RoleProvider的类。 The only methods you need to implement are IsUserInRole and GetRolesForUser. 您需要实现的唯一方法是IsUserInRole和GetRolesForUser。 You may just throw a NotImplementedException on all the other methods. 您可能只是在所有其他方法上抛出NotImplementedException。 Then, tie it to your application in Web.Config by setting the roleManager element under System.Web. 然后,通过在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
}

and then, in Web.Config 然后,在Web.Config中

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

DISCLAIMER: There are likely typos in this code but you should be able to get the gyst 免责声明:此代码中可能存在拼写错误,但您应该能够获得这个问题

There is a membership provider created specifically for ActiveDirectory: 有专门为ActiveDirectory创建的成员资格提供程序:

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

You can implement that provider in your app. 您可以在应用中实施该提供商。 In addition, you can create your own membership provider if you need additional functionality that the ActiveDirectoryMembershipProvider does not provide: 此外,如果您需要ActiveDirectoryMembershipProvider未提供的其他功能,您可以创建自己的成员资格提供程序:

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

Asp.Net Identity separates Identity and Authorization as two distinct components. Asp.Net Identity将Identity和Authorization分为两个不同的组件。

By design you can choose to use the AD identity piece with the Asp.Net Authorization piece. 通过设计,您可以选择将AD标识片与Asp.Net授权片一起使用。 Such that you can use the local AD token to identify WHO the user is and then use that token to assign them privileges (roles and/or claims) based on that identity. 这样您就可以使用本地AD令牌来识别用户所在的WHO,然后使用该令牌根据该身份为其分配权限(角色和/或声明)。 Similar to how you can also use Google, Facebook or Twitter identities. 与您如何使用Google,Facebook或Twitter身份类似。 Obviously, if your AD authorities won't allow you to query AD for "who is the user behind token X" then this answer is moot. 显然,如果您的AD权限不允许您查询AD“谁是令牌X后面的用户”,那么这个答案没有实际意义。

I haven't time to go further with this right now, but I think this should start you in the right direction. 我现在没有时间进一步研究这个问题,但我认为这应该让你朝着正确的方向前进。

(caveat: you MAY become limited to using a Microsoft browser. Last I looked only IE would send the Active Directory Token with the HttpRequest IF the request was being sent to a local domain server (aka the 'intranet' zone). I have heard that Chrome will allow you to configure it do this as well, but have never actually done it.) (请注意:您可能只能使用Microsoft浏览器。最后我看起来只有IE会发送带有HttpRequest的Active Directory令牌,如果请求被发送到本地域服务器(也就是“内部网”区域)。我听说过Chrome将允许您配置它也执行此操作,但从未实际执行过此操作。)

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

相关问题 在Windows身份验证中使用asp.net中的角色 - using roles in asp.net with windows authentication 如何在ASP.NET Core中将角色添加到Windows身份验证 - How to add Roles to Windows Authentication in ASP.NET Core 在Windows身份验证模式下为c#asp.net应用程序中的特定页面授予特定组的访问权限 - Giving access to particular groups certain pages in c# asp.net application in windows authentication mode 通过在ASP.NET应用程序中通过Windows身份验证 - By Passing Windows Authentication in ASP.NET application ASP.NET中的自定义角色 - Custom roles in ASP.NET 用于 AD 组的 ASP.NET 核心 5 MVC 上的 Windows 身份验证 - Windows authentication on ASP.NET Core 5 MVC for AD groups 角色身份验证在 asp.net 中不起作用 - Roles authentication is not working in asp.net 通过存储过程进行 Windows 身份验证和角色身份验证 - mvc asp.net - Windows authentication and roles authentication through stored procedure - mvc asp.net ASP.NET MVC应用程序中Windows身份验证失败的自定义错误页面 - Custom error page for failed windows authentication in ASP.NET MVC application ASP.NET角色身份验证-使用角色作为活动? - ASP.NET Roles Authentication - Using Roles as Activities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM