gpt4 book ai didi

c# - 尝试创建 PrincipalContext 时代码崩溃

转载 作者:行者123 更新时间:2023-11-30 22:21:27 27 4
gpt4 key购买 nike

每当调用此方法时,我都会崩溃。

    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;
}

内部异常说

{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

堆栈跟踪是

 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");
}

如果我删除/注释代码,它会编译并运行良好。


更新:在听从 DJ KRAZE 的建议之后

const string Domain = "someserver";

将 PrincipalContext 更改为接受 2 个协议(protocol)后,新的 StackTrace 是

      at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

最佳答案

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,字符串域名;

您可以遵循的硬编码示例

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);

关于c# - 尝试创建 PrincipalContext 时代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14364056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com