Tag: directoryservices

如何列出所有计算机以及他们最后一次登录AD?

我正在尝试检索计算机名称列表以及它们上次从Active Directory登录的日期,并将它们返回到数据表中。 获取名称很容易但是当我尝试添加“lastLogon”或“lastLogonTimestamp”时,如下所示,我为lastLogonTimestamp获取的唯一值是“System._ComObject” public DataTable GetListOfComputers(string domainName) { DirectoryEntry entry = new DirectoryEntry(“LDAP://DC=” + domainName + “,DC=com”); DirectorySearcher search = new DirectorySearcher(entry); string query = “(objectclass=computer)”; search.Filter = query; search.PropertiesToLoad.Add(“name”); search.PropertiesToLoad.Add(“lastLogonTimestamp”); SearchResultCollection mySearchResultColl = search.FindAll(); DataTable results = new DataTable(); results.Columns.Add(“name”); results.Columns.Add(“lastLogonTimestamp”); foreach (SearchResult sr in mySearchResultColl) { DataRow dr = results.NewRow(); DirectoryEntry de = […]

UserPrincipal.GetAuthorizationGroups()方法出错

我在Web应用程序中使用UserPrincipal类的GetAuthorizationGroups方法时遇到问题。 使用以下代码,我收到“尝试检索授权组时,发生错误(5)” PrincipalContext context = new PrincipalContext(ContextType.Domain, null, “DC=MyCompany,DC=COM”, “username”, “password”); UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, “joe.blogs”); var groups = p.GetAuthorizationGroups(); 我相信这段代码在某种程度上有效。 当我查看上下文对象时,我可以看到服务器和用户名/密码已在对象中正确解析 当我查看p对象时,我可以看到已经填充了AD详细信息,如电话号码等。 这是错误的堆栈跟踪。 [PrincipalOperationException: While trying to retrieve the authorization groups, an error (5) occurred.] System.DirectoryServices.AccountManagement.AuthZSet..ctor(Byte[] userSid, NetCred credentials, ContextOptions contextOptions, String flatUserAuthority, StoreCtx userStoreCtx, Object userCtxBase) +317279 System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p) +441 System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper() +78 […]

如何在复杂环境中使用FQDN获取NETBIOS域名

从完全限定的Active Directory域名获取NETBIOS域名有时是一项繁琐的工作。 我在这里找到了一个好的答案。 在具有多个林的环境中,如果PC不在您正在查询的林中,则此方法将不起作用。 这是因为LDAP://RootDSE将查询计算机域的信息。 有人可能会问:为什么这么复杂? 只需在检索到的第一个点之前使用该名称: ActiveDirectory.Domain.GetComputerDomain().Name; 或者只是获取用户的域名: Environment.GetEnvironmentVariable(“USERDOMAIN”); 要么 Environment.UserDomainName; 但是NETBIOS域名可能完全不同,您或您的计算机可能位于不同的域或林中! 所以这种方法只能在简单的环境中使用。 DJ KRAZE的解决方案只需要一个小的修改就可以进行跨域查询。 这假设了一种信任关系! private string GetNetbiosDomainName(string dnsDomainName) { string netbiosDomainName = string.Empty; DirectoryEntry rootDSE = new DirectoryEntry(string.Format(“LDAP://{0}/RootDSE”,dnsDomainName)); string configurationNamingContext = rootDSE.Properties[“configurationNamingContext”][0].ToString(); DirectoryEntry searchRoot = new DirectoryEntry(“LDAP://cn=Partitions,” + configurationNamingContext); DirectorySearcher searcher = new DirectorySearcher(searchRoot); searcher.SearchScope = SearchScope.OneLevel; searcher.PropertiesToLoad.Add(“netbiosname”); searcher.Filter = string.Format(“(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))”, dnsDomainName); […]

创建使用.NET 4.0的应用程序池

我使用以下代码创建一个应用程序池: var metabasePath = string.Format(@”IIS://{0}/W3SVC/AppPools”, serverName); DirectoryEntry newpool; DirectoryEntry apppools = new DirectoryEntry(metabasePath); newpool = apppools.Children.Add(appPoolName, “IIsApplicationPool”); newpool.CommitChanges(); 如何指定应用程序池应使用.NET Framework 4.0?

PrincipalSearcher和DirectorySearcher之间的区别

我看到使用PrincipalSearcher Active Directory示例以及执行相同操作但使用DirectorySearcher其他示例。 这两个例子有什么区别? 使用PrincipalSearcher示例 PrincipalContext context = new PrincipalContext(ContextType.Domain); PrincipalSearcher search = new PrincipalSearcher(new UserPrincipal(context)); foreach( UserPrincipal user in search.FindAll() ) { if( null != user ) Console.WriteLine(user.DistinguishedName); } 使用DirectorySearcher示例 DirectorySearcher search = new DirectorySearcher(“(&(objectClass=user)(objectCategory=person))”); search.PageSize = 1000; foreach( SearchResult result in search.FindAll() ) { DirectoryEntry user = result.GetDirectoryEntry(); if( null != user ) […]

限制LDAP查询中返回的属性

如何通过System.DirectoryServices限制LDAP查询中返回的属性? 我一直在使用DirectorySearcher并将我想要的属性添加到DirectorySearcher.PropertiesToLoad。 问题是,这只是确保添加的属性包含在DirectoryEntry.Properties以及一些默认列表中。 有没有办法指定您想要返回的唯一属性? DirectoryEntry base = new DiectoryEntry(rootPath, null, null, AuthenticationTypes.FastBind); DirectorySearcher groupSearcher = new DirectorySearcher(base); groupSearcher.Filter = “(objectClass=group)”; groupSearcher.PropertiesToLoad.Add(“distinguishedName”); groupSearcher.PropertiesToLoad.Add(“description”); foreach (SearchResult groupSr in groupDs.FindAll()) … 在foreach循环中,当我获得组DirectoryEntry时,我可以访问大约16种不同的属性,而不仅仅是我指定的两个属性(distinguishedName,description)

DirectoryServices UserPrincipal.SetPassword忽略密码策略(密码历史记录)

正如标题所示,我在设置用户密码时遇到了关于尊重密码策略的问题,特别是密码历史记录限制。 当用户不知道他当前的密码时,该方案是用户密码重置。 我使用以下内容来完成此任务: using (PrincipalContext context = new PrincipalContext(ContextType.Domain, “XXXX”, “ADMINUSER”, “ADMINPASSWORD”)) { using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)) { user.SetPassword(password); } } 这适用于每个策略MINUS密码历史记录限制。 现在采取这种情况,当用户想要更改他们的密码并知道我正在使用的当前密码时: using (PrincipalContext context = new PrincipalContext(ContextType.Domain, “XXXX.XXX.com”)) { using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)) { user.ChangePassword(currentPassword, newPassword); } } …按预期工作,并validation所有密码策略限制。 有没有人不得不这样做? 干杯:)

ASP.NET Core中的目录服务和帐户管理问题

我有一个ASP.NET核心API项目,我想利用DirectoryServices和DirectoryServices.AccountManagement命名空间。 经过一些研究,我认为我找到了一些可行的project.json引用: { “version”: “1.0.0-*”, “compilationOptions”: { “emitEntryPoint”: true }, “dependencies”: { “EntityFramework.Commands”: “7.0.0-rc1-final”, “EntityFramework.MicrosoftSqlServer”: “7.0.0-rc1-final”, “EntityFramework.MicrosoftSqlServer.Design”: “7.0.0-rc1-final”, “Microsoft.AspNet.IISPlatformHandler”: “1.0.0-rc1-final”, “Microsoft.AspNet.Mvc”: “6.0.0-rc1-final”, “Microsoft.AspNet.Server.Kestrel”: “1.0.0-rc1-final”, “Microsoft.AspNet.StaticFiles”: “1.0.0-rc1-final”, “Microsoft.Extensions.Configuration.FileProviderExtensions”: “1.0.0-rc1-final”, “Microsoft.Extensions.Configuration.Json”: “1.0.0-rc1-final”, “Microsoft.Extensions.Logging”: “1.0.0-rc1-final”, “Microsoft.Extensions.Logging.Console”: “1.0.0-rc1-final”, “Microsoft.Extensions.Logging.Debug”: “1.0.0-rc1-final” }, “commands”: { “web”: “Microsoft.AspNet.Server.Kestrel”, “ef”: “EntityFramework.Commands” }, “frameworks”: { “dnx451”: { “frameworkAssemblies”: { “System.DirectoryServices”: “4.0.0.0”, “System.DirectoryServices.AccountManagement”: “4.0.0.0” } […]

如何使用PrincipalContext搜索全局编录(整个林)

myUserList AppUsers = new myUserList(); using (PrincipalContext pcxt = new PrincipalContext(ContextType.Domain, domainName)) { UserPrincipal User = new UserPrincipal(pcxt); User.EmailAddress = emailString; PrincipalSearcher srch = new PrincipalSearcher(User); foreach (var principal in srch.FindAll()) { var p = (UserPrincipal)principal; myUserRow User = AppUsers.NewUsersRow(); User.FirstName = p.GivenName; User.LastName = p.Surname; User.Email = p.EmailAddress; AppUsers.AddUsersRow(User); } } 我有类似于上面的代码,使用PrincipalContext类在Active Directory中搜索用户信息。 正如您所看到的,我在搜索过程中传入了domainName。 […]

如何使用C#确定虚拟目录或网站的ASP.NET版本?

我如何找到IIS虚拟目录的.NET框架正在C#中使用。 我需要将它显示给用户。 using System.DirectoryServices; using System.IO; private enum IISVersion { IIS5, IIS6 } private void ReadVirtualDirectory(string server, string directory, IISVersion version) { string siteID = string.Empty; string path = string.Empty; switch(verison) { case IISVersion.IIS5: path = string.Format(“IIS://{0}/W3SVC/1/Root”, server); if(!string.IsNullOrEmpty(directory)) { path = string.Concat(path, “/”, directory); } break; case IISVersion.IIS6: path = string.Format(“IIS://{0}/W3SVC”, server); if(!string.IsNullOrEmpty(directory)) { […]