我们已将 IIS(比如说 myIIS.xx1.mydomain.com)从 .NET 4 更新到 4.5 更新后,我们无法从我们的某个域(比如说 xx3.mydomain.com)获取用户。从其他人(比如说 xx1.mydomain.com、xx2.mydomain.com、xx5.mydomain.com)我们仍然可以得到用户。但它适用于 .NET 4 上的所有域
我们习惯于跟随代码来测试它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.Security.Principal;
namespace ADTestApp
{
class Program
{
static void Main(string[] args)
{
bool exit = false;
do {
Console.WriteLine(".NET Version: " + (IsNet45OrNewer() ? "4.5" : "4"));
Console.WriteLine("enter search query");
string searchQuery = Console.ReadLine();
Console.WriteLine("querying global catalog...");
string adServer = "mydomain.com:3268";
string adContainer = "DC=mydomain,DC=com";
string serviceAccountUserName = "xx5\\myusername";
string serviceAccountPW = "mypassword";
List<string> users = new List<string>();
PrincipalContext principalContext = new PrincipalContext(
ContextType.Domain,
adServer,
adContainer,
serviceAccountUserName,
serviceAccountPW);
CustomUserPrincipal user = new CustomUserPrincipal(principalContext) { EmailAddress = searchQuery, Enabled = true };
PrincipalSearcher searcher = new PrincipalSearcher() { QueryFilter = user };
foreach (UserPrincipal p in searcher.FindAll())
{
try
{
if (p.EmailAddress != null && p.Surname != null && p.GivenName != null)
{
users.Add(p.Surname + ", " + p.GivenName + " " + p.MiddleName + " - " + p.EmailAddress);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
if (users.Count > 0)
{
Console.WriteLine("Results:");
foreach (string usr in users)
{
Console.WriteLine(usr);
}
}
else
{
Console.WriteLine("no results found");
}
}
while(exit == false);
}
public static bool IsNet45OrNewer()
{
// Class "ReflectionContext" exists from .NET 4.5 onwards.
return Type.GetType("System.Reflection.ReflectionContext", false) != null;
}
}
}
'xx3.mydomain.com'(不再起作用的那个)抛出以下异常:
在 System.DirectoryServices.AccountManagement.UserPrincipal.get_EmailAddress()
对我来说,这似乎是一个访问问题。但是如果客户端上安装了 .NET 4,我仍然可以访问该域。我已经在多个域中的多个客户端和服务器上对其进行了测试,但是在所有使用 .NET 4.5 的客户端上,这个特定域都不起作用。
高度赞赏帮助。提前感谢您的任何反馈和建议。