每当我打电话给这个方法时,我都会崩溃。
public bool findSAMAccountByName(string environmentUserName)
{
try
{
const string Domain = "someserver";
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, environmentUserName);
if (user != null)
{
// do something here....
string samAccountName = user.SamAccountName;
return true;
}
return false;
}
catch (Exception e)
{
e.ToString();
}
return false;
}
内在感觉说
{“尝试读取或写入受保护的内存。这通常是一个 表明其他内存已损坏。“}
StackTrace是
at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetEx(String bstrName, Object& value)
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)
at ActiveRecord.MainWindow.findSAMAccountByName(String environmentUserName) in MainWindow.xaml.cs:line 60
at ActiveRecord.MainWindow..ctor() in MainWindow.xaml.cs:line 39
我对此方法的调用导致崩溃
if (findSAMAccountByName(Environment.UserName))
{
Console.WriteLine("Username found");
}
如果我删除/注释代码,它会编译并运行正常。
const string Domain = "someserver";
将PrincipalContext更改为接受2个agruements后,新的StackTrace为
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
答案 0 :(得分:3)
这样的Cocoa Dev应该可以工作..
const string Domain = "SomeServer.Foo.Bar.com";
const string Username = "someUserSamAccountName";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, Username);
替换为您的值,如果找到用户,则应该返回..我每天都使用AD并且只使用我的帐户信用卡测试希望这有助于如果这样改变您的方法传递字符串samAccount,string DomanName;
您可以遵循的硬编码示例
const string Domain = "SLO1.Foo.Bar.biz:389";
const string Container = @"DC=Foo,DC=Bar,DC=biz";
const string Username = @"cocoadev";//replace with actual samAccountName
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);