简体   繁体   English

Active Directory组和C#网页配置

[英]Active Directory Group and C# webpage configuration

I am wanting to allow access to a C# Webpage to only members in an Active Directory group. 我想只允许Active Directory组中的成员访问C#网页。 Can someone please point me in this direction or assist in anyway? 有人可以指出我的方向还是提供协助?

Thanks in advance 提前致谢

You can query AD to see what groups a user belongs to. 您可以查询AD以查看用户所属的组。

This is a great resource: http://www.codeproject.com/KB/system/everythingInAD.aspx#39 这是一个很好的资源: http : //www.codeproject.com/KB/system/everythingInAD.aspx#39

Something like this should work too: 这样的事情也应该起作用:

using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices;
public bool IsUserInGroup(string group, string user)
{
    string DomainName="";
    string ADUsername="";
    string ADPassword="";

    DirectoryEntry entry=new DirectoryEntry(LDAPConnectionString, ADUsername, ADPassword);
    DirectorySearcher dSearch=new DirectorySearcher(entry);
    dSearch.Filter="(&(objectClass=user)(userPrincipalName=" + user + ")";

    foreach(SearchResult sResultSet in dSearch.FindAll())
    {
        string strGroupList=GetProperty(sResultSet, "memberOf");
        if(!string.IsNullOrEmpty(strGroupList) && strGroupList.IndexOf(group)>-1)
            return true;
    }
    return false;
} 

I didn't have time to check this or even compile, so I apologize in advance for any error. 我没有时间检查甚至编译,因此对于任何错误我都表示歉意。 The if in the foreach might not be sufficient. foreachif可能不足。 There also may be a more efficient way to do the query, but this was what I could come up with quickly. 也许还有一种更有效的查询方法,但这是我可以很快想到的。

There exist multiple approaches to this. 存在多种方法。

Imperatively, you can check Page.User.IsInRole(@"domain\\group"), and redirect away, send a 401 response, or throw an exception if the user should not have access. 强制性地,您可以检查Page.User.IsInRole(@“ domain \\ group”),然后重定向,发送401响应,或者如果用户不具有访问权限,则引发异常。

Declaratively, you can control permissions in your web.config : 声明性地,您可以在web.config中控制权限

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

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