C# 使用当前用户';s SharePoint凭据以获得Active Directory访问权限
我正在开发SharePoint 2010 Web部件(用C#)它需要从Active Directory中提取用户数据,但我在使用当前SharePoint用户的凭据向AD进行身份验证时遇到问题。我使用DirectoryEntry类从AD获取数据,当我硬编码某些凭据时,它可以正常工作,但我知道这是一种不好的做法,因此我希望避免。我试过几种不同的方法,比如: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
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对象模型获取用户数据。在谷歌上搜索了一下双跳问题,听上去肯定是这个问题。因为我们已经设置好了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"];
// ...
}
}
}
}
}