Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Windows身份验证用户填充下拉列表?_C#_Asp.net Mvc 3_Model View Controller_Windows Authentication - Fatal编程技术网

C# 使用Windows身份验证用户填充下拉列表?

C# 使用Windows身份验证用户填充下拉列表?,c#,asp.net-mvc-3,model-view-controller,windows-authentication,C#,Asp.net Mvc 3,Model View Controller,Windows Authentication,我在寻找这个的过程中两手空空。有人有答案吗?使用MVC3/C#。谢谢。一种方法是使用从active directory请求用户列表 更新 这是我们使用的类;您可以调用FetchContacts静态方法来检索用户: using System; using System.Collections.Generic; using System.DirectoryServices; public class ActiveDirectorySystem { private const string

我在寻找这个的过程中两手空空。有人有答案吗?使用MVC3/C#。谢谢。

一种方法是使用从active directory请求用户列表

更新

这是我们使用的类;您可以调用FetchContacts静态方法来检索用户:

using System;
using System.Collections.Generic;
using System.DirectoryServices;

public class ActiveDirectorySystem
{
    private const string PROPERTY_DISPLAY_NAME = "displayName";

    private static DirectorySearcher Initialize()
    {
        // Exceptions are handled by the caller

        // Obtain the domain root entry
        using (DirectoryEntry theRootEntry = new DirectoryEntry("LDAP://RootDSE"))
        {
            object theNamingContext = null;
            string sNamingContext = null;

            // Verify that we retrieved it correctly and raise an error if we did not
            if (theRootEntry == null)
            {
                throw new Exception("A directory services entry for the LDAP RootDSE could not be created.");
            }

            // Get the root naming context
            theNamingContext = theRootEntry.Properties["rootDomainNamingContext"].Value;
            // Verify that we retrieved it correctly and raise an error if we did not
            if ((theNamingContext == null) || (theNamingContext.ToString().Length == 0))
            {
                throw new Exception("The root domain naming context property could not be retrieved from the LDAP directory services");
            }
            else
            {
                sNamingContext = theNamingContext.ToString();
            }

            // And create a new directory entry for the root naming context
            using (DirectoryEntry theEntry = new DirectoryEntry("LDAP://" + sNamingContext))
            {
                // Verify that we retrieved it correctly and raise an error if we did not
                if (theEntry == null)
                {
                    throw new Exception("A directory entry object could not be created for LDAP://" + sNamingContext);
                }

                // Now we configure what we are looking for from Active Directory
                DirectorySearcher oSearcher = null;

                // Start with a new searcher for the root domain
                oSearcher = new DirectorySearcher(theEntry);
                // Verify that we retrieved it correctly and raise an error if we did not
                if (oSearcher == null)
                {
                    throw new Exception("A directory searcher object could not be created for LDAP://" + sNamingContext);
                }

                // And the properties we want to retrieve
                oSearcher.PropertiesToLoad.Add(PROPERTY_DISPLAY_NAME);

                return oSearcher;
            }
        }
    }

    /// <summary>
    /// This method retrieves the set of users from active directory that meet the criteria specified in filter.
    /// </summary>
    /// <param name="sFilter"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    public static List<string> FetchContacts(string sFilter = "")
    {
        // Exceptions are handled by the caller

        using (DirectorySearcher oSearcher = Initialize())
        {
            if (oSearcher != null)
            {
                List<string> cNames = new List<string>();

                // Specify what we are looking for, which is the account name of the specified user without any domain information
                oSearcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user){0})", sFilter);

                // get all of the matching records
                using (SearchResultCollection cResults = oSearcher.FindAll())
                {
                    if (cResults != null)
                    {
                        foreach (SearchResult theCurrentResult in cResults)
                        {
                            System.DirectoryServices.ResultPropertyCollection oProperties = null;

                            oProperties = theCurrentResult.Properties;
                            // First, verify that at least the display name is contained in the result
                            if (oProperties.Contains(PROPERTY_DISPLAY_NAME) && oProperties[PROPERTY_DISPLAY_NAME].Count > 0)
                            {
                                cNames.Add(oProperties[PROPERTY_DISPLAY_NAME][0].ToString());
                            }
                        }
                    }
                }

                return cNames;
            }
        }

        return null;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.DirectoryServices;
公共类ActiveDirectorySystem
{
私有常量字符串属性\u DISPLAY\u NAME=“displayName”;
私有静态DirectorySearcher Initialize()
{
//异常由调用方处理
//获取域根条目
使用(DirectoryEntry theRootEntry=newdirectoryEntry(“LDAP://RootDSE”))
{
对象名为namingContext=null;
字符串sNamingContext=null;
//验证我们是否正确检索了它,如果没有,则引发错误
如果(theRootEntry==null)
{
抛出新异常(“无法创建LDAP RootDSE的目录服务条目”);
}
//获取根命名上下文
NamingContext=rootentry.Properties[“rootDomainNamingContext”].Value;
//验证我们是否正确检索了它,如果没有,则引发错误
if((theNamingContext==null)| |(theNamingContext.ToString().Length==0))
{
抛出新异常(“无法从LDAP目录服务检索根域命名上下文属性”);
}
其他的
{
sNamingContext=theNamingContext.ToString();
}
//并为根命名上下文创建一个新的目录项
使用(DirectoryEntry theEntry=newdirectoryentry(“LDAP://“+sNamingContext))
{
//验证我们是否正确检索了它,如果没有,则引发错误
如果(entry==null)
{
抛出新异常(“无法为LDAP://“+sNamingContext创建目录条目对象”);
}
//现在,我们从Active Directory配置要查找的内容
DirectorySearcher-oSearcher=null;
//从根域的新搜索器开始
oSearcher=新的董事搜索者(theEntry);
//验证我们是否正确检索了它,如果没有,则引发错误
if(oSearcher==null)
{
抛出新异常(“无法为LDAP://“+sNamingContext创建目录搜索程序对象”);
}
//以及我们要检索的属性
oSearcher.PropertiesToLoad.Add(属性显示名称);
返回弓箭手;
}
}
}
/// 
///此方法从active directory检索满足筛选器中指定条件的用户集。
/// 
/// 
/// 
/// 
公共静态列表获取联系人(字符串sFilter=“”)
{
//异常由调用方处理
使用(DirectorySearcher oSearcher=Initialize())
{
if(oSearcher!=null)
{
List cNames=新列表();
//指定我们要查找的内容,即不包含任何域信息的指定用户的帐户名
oSearcher.Filter=string.Format(&(objectCategory=person)(objectClass=user){0}),sFilter;
//获取所有匹配的记录
使用(SearchResultCollection cResults=oSearcher.FindAll())
{
如果(结果!=null)
{
foreach(搜索结果当前结果为结果)
{
System.DirectoryServices.ResultPropertyCollection OpProperties=null;
oProperties=当前结果属性;
//首先,验证结果中至少包含显示名称
if(oProperties.Contains(PROPERTY\u DISPLAY\u NAME)&&oProperties[PROPERTY\u DISPLAY\u NAME]。计数>0)
{
添加(操作属性[属性显示名称][0].ToString());
}
}
}
}
返回cNames;
}
}
返回null;
}
}

我不太了解如何获取数据。你有什么例子吗?谢谢。@Ber53rker:用我们使用的类的精简版本更新了答案。太好了!谢谢有点让它工作了。现在我只需要弄清楚如何只让特定群体的人。。。