Tag: directoryservices

WCF服务中的System.DirectoryServices.AccountManagement.PrincipalContext和Impersonation

在位于WCF服务后面的代码中使用PrincipalContext 。 WCF服务正在模拟,以允许“传递”类型身份validation。 虽然我使用Active Directory(主要是System.DirectoryServices.Protocols命名空间)所做的其他事情在这种情况下工作正常,但由于某种原因,System.DirectoryServices.AccountManagement中的类会使用。 失败的示例代码: PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName); UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, UserName); 当我调用FindByIdentity ,我得到一个COMException:“发生了一个操作错误”。 对PrincipalContext调用也会失败,例如: string server = context.ConnectedServer; OperationContext.Current.ServiceSecurityContext和Thread.CurrentPrincipal.Identity显示模拟正常工作。 并且,就像我说的那样,S.DS.P中的其他AD任务工作正常。 如果我在PrincipalContext上显式设置凭据,一切正常。 例如: PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName, user, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, UserName); 现在一切正常。 但我不知道来电者的用户名和密码; 我必须依靠冒充。 关于什么会引起我所看到的问题的任何想法? 提前致谢! 詹姆士

调用commitChanges()在Active Directory中什么都不做?

尽管我使用了CommitChanges函数,但似乎在ActiveDirectory中没有保存更改。 我是否使用正确的方法来解决这个问题? //Test OU Group: OU=First Group,OU=Domain Users,DC=DOMAIN,DC=com String userName, password; Console.Write(“Username: “); userName = Console.ReadLine(); Console.Write(“Password: “); password = Console.ReadLine(); //ENTER AD user account validation code here String strLDAPpath = “LDAP://OU=First Group,OU=Domain Uers,DC=DOMAIN,DC=com”; DirectoryEntry entry = new DirectoryEntry(strLDAPpath,userName,password,AuthenticationTypes.Secure); //DirectoryEntry entry = new DirectoryEntry(strLDAPpath); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = “(ObjectCategory=user)”; foreach (SearchResult result […]

使用PrincipalSearcher.FindAll()时内存泄漏

我也有使用插件和appdomains的长时间运行服务,并且由于使用目录服务而导致内存泄漏。 请注意,我使用的是system.directoryservices.accountmanagement,但我理解它使用相同的底层ADSI API,因此容易出现相同的内存泄漏。 我查看了所有CLR内存计数器,并且内存没有泄漏,并且都是在强制GC或卸载appdomain时返回的。 泄漏是私有字节,不断增长。 我在这里搜索并看到了一些与使用ADSI API时内存泄漏相关的问题,但它们似乎表明只需遍历directorysearcher即可解决问题。 但正如您在下面的代码中看到的那样,我在foreach块中执行此操作,但内存仍然被泄露。 有什么建议? 这是我的方法: public override void JustGronkIT() { using (log4net.ThreadContext.Stacks[“NDC”].Push(GetMyMethodName())) { Log.Info(“Inside ” + GetMyMethodName() + ” Method.”); System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader(); //PrincipalContext AD = null; using (PrincipalContext AD = new PrincipalContext(ContextType.Domain, (string)reader.GetValue(“Domain”, typeof(string)))) { UserPrincipal u = new UserPrincipal(AD); u.Enabled = true; //u.Surname = “ju*”; using […]

扩展GroupPrincipal和Members属性

我想扩展GroupPrincipal类来处理一些自定义属性: using System.DirectoryServices.AccountManagement; [DirectoryRdnPrefix(“CN”)] [DirectoryObjectClass(“group”)] public class MyGroupPrincipal : GroupPrincipal { // … } 我如何覆盖MyGroupPrincipal的Members属性,以便如果它有一个组成员,则返回MyGroupPrincipal而不是GroupPrincipal的实例? 我想写一下 MyGroupPrincipal group = GetGroup(); foreach (var m in group.Members) { if (m is MyGroupPrincipal) { // always fails: m is a normal GroupPrincipal // do something } }

使用PrincipalContext和ADLDS,LDAP服务器不可用

我们正在利用ADLDS进行用户管理和身份validation。 我们可以毫无问题地成功查询实例。 但是,如果未设置密码,尝试执行SetPassword等操作将失败甚至尝试创建新用户,则会失败。 我可以成功更新用户,只要它不是我想要更新的密码。 我一直在阅读很多与此相关的不同文章,但没有找到解决方案。 发帖看看我是否可以对这个问题有一些新的看法,感谢任何意见。 例 ContextType ctxType = ContextType.ApplicationDirectory; string server = “myadldsserver.com”; string usersCN = “CN=Users,…”; // container where users reside ContextOptions ctxOpts = ContextOptions.SimpleBind; string uname = “myuser”; string pswrd = “mypass”; using(var ctx = new PrincipalContext(ctxType, server, usersCN, ctxOpts, uname, pswrd) using(var newUser = new UserPrincipal(ctx)) { newUser.Name = “newusername”; […]

如何使用System.DirectoryServices.AccountManagement将用户添加到AD?

使用.net 3.5框架和C#我试图从C#向AD添加新用户,但找不到任何示例。 我看到PrincipalCollection对象有一个重载的’add’方法,但似乎无法弄清楚它是如何工作的。 有人可以帮忙吗? 如何创建新的用户对象,将其添加到AD中。 其次,将要添加新用户的用户可能实际上没有安全性来执行此操作。 有没有办法可以冒充其他具有权限的用户帐户并以这种方式添加帐户?

如何使用objectGUID获取DirectoryEntry?

我知道,我们可以像这样得到一个DirectoryEntry: string conPath = “LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com”; string conUser = “administrator”; string conPwd = “Iampassword”; DirectoryEntry de = new DirectoryEntry(conPath, conUser, conPwd, AuthenticationTypes.Secure); 我们可以像这样更改用户的密码: DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = String.Format(“sAMAccountName={0}”, “xumai”); SearchResultCollection results = deSearch.FindAll(); foreach (SearchResult objResult in results) { DirectoryEntry obj = objResult.GetDirectoryEntry(); obj.Invoke(“setPassword”, new object[] { “Welcome99” }); obj.CommitChanges(); […]

模拟DirectoryEntry的“属性”属性

我正在尝试对一些Active Directory代码进行unit testing,与此问题中概述的完全相同: 创建DirectoryEntry实例以用于测试 接受的答案建议为DirectoryEntry类实现一个包装器/适配器,我有: public interface IDirectoryEntry : IDisposable { PropertyCollection Properties { get; } } public class DirectoryEntryWrapper : DirectoryEntry, IDirectoryEntry { } 问题是我的IDirectoryEntry mock上的“ Properties ”属性未初始化。 试图像这样设置模拟: this._directoryEntryMock = new Mock(); this._directoryEntryMock.Setup(m => m.Properties) .Returns(new PropertyCollection()); 导致以下错误: 类型’System.DirectoryServices.PropertyCollection’没有定义构造函数 据我所知,当尝试仅使用内部构造函数实例化一个类时,会抛出此错误: 类型’…’没有定义构造函数 我试图为PropertyCollection类编写一个包装器/适配器但是没有公共构造函数我无法弄清楚如何从类中实例化或inheritance。 那么我如何在DirectoryEntry类上模拟/设置“ Properties ”属性以进行测试呢?

为什么这会抓住所有块实际上并不是全部

代码非常简单—问题是groupPath字符串中有一个无效字符(确切地说是’/’)。 我想要做的事情(至少作为一个停止差距)是跳过DirectoryEntries我无法获得cn – 无论为什么。 但是,当我运行此代码时,catch块不会运行,而是我得到:服务器无法运行。 和未处理的System.Runtime.InteropServices.COMException。 为什么catch阻止不会捕获此exception。 try { using (DirectoryEntry groupBinding = new DirectoryEntry(“LDAP://” + groupPath)) { using (DirectorySearcher groupSearch = new DirectorySearcher(groupBinding)) { using (DirectoryEntry groupEntry = groupSearch.FindOne().GetDirectoryEntry()) { results.Add(string.Format(“{0}”, groupEntry.Properties[“cn”].Value.ToString())); } } } } catch { Logger.Error(“User has bad roles”); } 附加观察:代码实际上是在一个自定义的RoleProvider中,好奇的是,如果我在一个简单的winforms应用程序中引用这个提供程序,并使用相同的输入调用这个相同的方法,那么catch块就会完全按照它的设想执行。 我认为这表明有关.NETexception与COMexception的建议答案并不准确。 虽然我无法理解为什么从WebDev服务器执行时此代码不会被捕获

尝试连接到远程IIS服务器时出现“拒绝访问” – C#

当我尝试从IIS 5.1下运行的C#应用​​程序连接到远程IIS 6服务器时,我收到“Access Deined”COMException。 有任何想法吗? 我遇到了原始问题的所有相同问题。 更新 – 4/1/09 我找到了这个解决方案( http://www.codeproject.com/KB/cs/Start_Stop_IIS_Website.aspx ),它包含一个连接到IIS服务器以启动和停止网站的窗口应用程序。 我能够在我的工作站上运行它并连接到IIS服务器。 呃….为什么我可以运行这个独立应用程序而不是我的ASP.NET应用程序? 原版的 当我尝试使用DirectoryEntry.Exist方法从远程计算机连接到IIS以检查IIS服务器是否有效时,我收到“拒绝访问”COMException。 string path = string.Format(“IIS://{0}/W3SVC”, server); if(DirectoryEntry.Exist(path)) { //do something is valid…. } 我是一个活动目录组的成员,该组已添加到管理员组到我尝试连接的IIS服务器。 有没有人遇到这个问题,知道如何解决它? 更新: @Kev – 这是一个ASP.NET应用程序。 此外,我可以通过IIS6管理器在没有用户名和密码的情况下连接到远程服务器。 @Chris – 我正在尝试连接到远程服务器以显示虚拟directorys的数量,并确定每个目录的.NET框架版本。 看到这个问题。 @dautzenb – 我的ASP.NET应用程序在IIS 5.1下运行,试图连接到IIS 6服务器。 我可以在远程服务器上的本地ASPNET帐户的安全日志中看到故障审核。 当我尝试调试应用程序时,我在我的域帐户下运行,但仍然拒绝访问。 更新2: @Kev – 我能够建立使用以下重载创建DirectoryEntry对象: public DirectoryEntry ( string path, […]