C# 使用当前用户';s SharePoint凭据以获得Active Directory访问权限

C# 使用当前用户';s SharePoint凭据以获得Active Directory访问权限,c#,sharepoint,active-directory,C#,Sharepoint,Active Directory,我正在开发SharePoint 2010 Web部件(用C#)它需要从Active Directory中提取用户数据,但我在使用当前SharePoint用户的凭据向AD进行身份验证时遇到问题。我使用DirectoryEntry类从AD获取数据,当我硬编码某些凭据时,它可以正常工作,但我知道这是一种不好的做法,因此我希望避免。我试过几种不同的方法,比如: new DirectoryEntry("LDAP://" + dc, null, null, AuthenticationTypes.Serve

我正在开发SharePoint 2010 Web部件(用C#)它需要从Active Directory中提取用户数据,但我在使用当前SharePoint用户的凭据向AD进行身份验证时遇到问题。我使用DirectoryEntry类从AD获取数据,当我硬编码某些凭据时,它可以正常工作,但我知道这是一种不好的做法,因此我希望避免。我试过几种不同的方法,比如:

new DirectoryEntry("LDAP://" + dc, null, null, AuthenticationTypes.ServerBind | AuthenticationTypes.FastBind);

但它们都只是抛出异常。我真的不知道这些是做什么的,但这是我在网上找到的


非常感谢任何能为我指明正确方向的帮助。

我正在使用此代码与当前用户连接到AD:

    using (DirectoryEntry entry = new DirectoryEntry())
    {
        using (DirectorySearcher searcher = new DirectorySearcher(entry))
        {
            searcher.Filter = "yourSearchQuery";
            searcher.PropertiesToLoad.Add("yourProperty1");
            searcher.PropertiesToLoad.Add("yourProperty2");

            using (SearchResultCollection results = searcher.FindAll())
            {
                foreach (SearchResult result in results)
                {
                    using (DirectoryEntry resultEntry = result.GetDirectoryEntry())
                    {
                        PropertyValueCollection valuesForProperty1 = resultEntry.Properties["yourProperty1"];
                        PropertyValueCollection valuesForProperty2 = resultEntry.Properties["yourProperty2"];

                        // ...
                    }
                }
            }
        }
    }

你可能遇到了臭名昭著的“双跳”问题。Sharepoint服务器无法在您访问另一台服务器时自动进行身份验证,即使是在同一域上。谷歌“双跳”获取更多关于这个问题的信息

有几个选项可供您选择:

  • 您可以使用Sharepoint 2010中的Secure Store Service存储连接到AD所需的用户名/密码,可以是所有Sharepoint用户共享的单个用户名/密码(类似于下面的#2),也可以是每个Sharepoint用户的用户名/密码(类似于下面的#3)

  • 您可以设置一个对AD具有只读权限的AD帐户,然后创建一些Web部件属性来存储用户的用户名和密码。设置属性,使其由所有用户共享,并由管理员设置。连接到AD时使用存储的用户名/密码。设置密码属性,使其仅从UI端写入

  • 如果对每个用户来说,根据用户在AD中的权限查看一组不同的AD数据很重要,那么您可以执行与上述类似的操作,但不要共享用户名/密码属性

  • 为Sharepoint环境设置Kerberos,从而允许Sharepoint web服务器在您进行AD时进行身份验证,而无需存储凭据

  • 设置AD以允许匿名读取访问(出于安全考虑,可能不是最好的)


  • 您是否配置了配置文件服务?你可以利用它来使用SharePoint对象模型获取用户数据。在谷歌上搜索了一下双跳问题,听上去肯定是这个问题。因为我们已经设置好了Kerberos,所以正在对此进行研究,尽管我的测试服务器似乎不受信任,无法进行委派。我要和我们的系统管理员聊聊。谢谢
        using (DirectoryEntry entry = new DirectoryEntry())
        {
            using (DirectorySearcher searcher = new DirectorySearcher(entry))
            {
                searcher.Filter = "yourSearchQuery";
                searcher.PropertiesToLoad.Add("yourProperty1");
                searcher.PropertiesToLoad.Add("yourProperty2");
    
                using (SearchResultCollection results = searcher.FindAll())
                {
                    foreach (SearchResult result in results)
                    {
                        using (DirectoryEntry resultEntry = result.GetDirectoryEntry())
                        {
                            PropertyValueCollection valuesForProperty1 = resultEntry.Properties["yourProperty1"];
                            PropertyValueCollection valuesForProperty2 = resultEntry.Properties["yourProperty2"];
    
                            // ...
                        }
                    }
                }
            }
        }