我正在尝试获取目录服务对象的"uSNChanged"值的Int64值.不幸的是,它总是以某种COM对象的形式回归.我尝试使用转换为Int64,调用Int64.Parse(),并调用Convert.ToInt64().这些都不起作用.
对于给定的DirectoryEntry对象,此代码将显示以下属性:
private static void DisplaySelectedProperties(DirectoryEntry objADObject)
{
try
{
string[] properties = new string[] {
"displayName",
"whenCreated",
"whenChanged",
"uSNCreated",
"uSNChanged",
};
Console.WriteLine(String.Format("Displaying selected properties of {0}", objADObject.Path));
foreach (string strAttrName in properties)
{
foreach (var objAttrValue in objADObject.Properties[strAttrName])
{
string strAttrValue = objAttrValue.ToString();
Console.WriteLine(String.Format(" {0, -22} : {1}", strAttrName, strAttrValue));
}
}
Console.WriteLine();
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Fatal error accessing: {0} - {1}", objADObject.Path, ex.Message), ex);
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Displaying selected properties of LDAP://server/o=org/cn=obj displayName : …
我正在尝试获取Windows登录对话框中可用的所有域(在域下拉列表中).
我尝试了以下代码但它只返回我登录的域.我错过了什么吗?
StringCollection domainList = new StringCollection();
try
{
DirectoryEntry en = new DirectoryEntry();
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher(en, "objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach( object domainName in resultPropColl["name"])
{
domainList.Add(domainName.ToString());
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return domainList;
Run Code Online (Sandbox Code Playgroud) 我正在为ASP.NET中的Web应用程序编写一个函数,其中客户端登录到服务器计算机,该计算机是针对服务器上的本地用户进行Windows身份验证的.我正在写的功能重置用户密码并通过电子邮件发送新密码.我是这样做的:
String userPath = "WinNT://" + Environment.MachineName + "/" + username.Text;
DirectoryEntry de = new DirectoryEntry(userPath);
de.Invoke("SetPassword", new object[] { password });
Run Code Online (Sandbox Code Playgroud)
我如何检查标志以强制用户在下次使用通过电子邮件发送给他们的密码登录时更改密码?我试过像这样使用pwdLastSet:
de.Properties["pwdLastSet"].Value = 0;
Run Code Online (Sandbox Code Playgroud)
但这显然只适用于LDAP,而不适用于WinNT,我在本地做这个.
任何专家都知道比我更好吗?我甚至试图通过命令行寻找一种方法来实现这一点,这样我就可以创建一个进程,但我也找不到这样做的方法.
我需要在AD DirectoryEntry中设置accountExpires属性找不到简单的答案.找到一些信息;
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/182bfb6a-8b23-4c96-9379-101a4d91241a
http://www.rlmueller.net/AccountExpires.htm
看到一些文章重新ADS****.dll但不认为我需要使用这种方法
Dim valueToSet As Date = Now.AddDays(10)
Dim ADSPath As String = "LDAP://cn=..."
Dim de As DirectoryEntry = New DirectoryEntry(ADSPath)
Dim d As TimeSpan = valueToSet.ToUniversalTime - Date.Parse("01/01/1601")
Dim ValueToSetAsString As String = d.Ticks.ToString
' it appears that the ticks value is too large for the value of the directory entry
' converting to a string (18 chars or so) works!
de.Properties("accountexpires").Value = ValueToSetAsString
Run Code Online (Sandbox Code Playgroud)
感谢Brian,看起来上面写的大量代码可以简化;
de.Properties("accountexpires").Value = valueToSet.ToFileTime.ToString
Run Code Online (Sandbox Code Playgroud)
在VB.NET中返回AccountExpires和其他largeInteger问题的函数
Function ConvertADValueToDateTime(ByVal li As Object) As …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
using (DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
{
DirectoryEntry NewUser = AD.Children.Add(username, "user");
string password = username + "123";
NewUser.Invoke("SetPassword", new object[] { password });
NewUser.CommitChanges();
NewUser.Close();
DirectoryEntry grp;
grp = AD.Children.Find(groupname, "group");
if (grp != null)
{
grp.Invoke("Add", new object[] { NewUser.Path.ToString() });
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建一个Windows用户并设置密码永不过期,但我不知道该怎么做?
有没有办法找出本地团体是否已经存在?我的"最佳"解决方案通过捕获异常来工作.有没有办法在没有捕获异常的情况下实现相同的目标?
var machine = Environment.MachineName;
var server = new DirectoryEntry(string.Format("WinNT://{0},Computer", machine));
bool groupExists = true;
try
{
server.Children.Find("MyGroup", "group");
}
catch (COMException ex)
{
if (ex.ErrorCode == -2147022676)
groupExists = false;
else
throw;
}
Run Code Online (Sandbox Code Playgroud) 我知道,我们可以像这样得到一个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);
Run Code Online (Sandbox Code Playgroud)
我们可以像这样更改用户的密码:
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();
}
Run Code Online (Sandbox Code Playgroud)
如果使用
string x = obj.Guid.ToString();;
Run Code Online (Sandbox Code Playgroud)
我们可以得到用户的objectGUID"0b118130-2a6f-48d0-9b66-c12a0c71d892"
我怎么能改变它是密码基础这个objectGUID?
如何搜索用户群这个objectGUID表单"LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com"?
是否有任何方式过滤它?etc strFilter ="(&(objectGUID = 0b118130-2a6f-48d0-9b66-c12a0c71d892))";
希望得到你的帮助
谢谢.
我正在尝试为指定OU中的每个AD用户修改配置文件/主目录/主驱动器设置,
我下面有一些非常基本的代码应该可以实现这一壮举,但是却抛出以下异常:
请求的操作不满足与该对象的类别相关联的一个或多个约束。
有没有人遇到过这个问题,如果有,有办法解决吗?
谢谢。
DirectoryEntry Entry = new DirectoryEntry("LDAP://OU=Company,DC=corp,DC=Placeholder,DC=com", null, null, AuthenticationTypes.Secure);
DirectorySearcher Searcher = new DirectorySearcher(Entry);
Searcher.SearchScope = SearchScope.Subtree;
Searcher.PropertiesToLoad.Add("sAMAccountName");
Searcher.Filter = "(&(objectClass=user)(objectCategory=person))";
foreach (SearchResult AdObj in Searcher.FindAll())
{
Entry.InvokeSet("HomeDirectory", @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]));
Entry.InvokeSet("HomeDrive", "H");
Entry.CommitChanges();
}
catch (Exception ex)
{
richTextBox1.Text += ex.Message;
}
Run Code Online (Sandbox Code Playgroud) 我想把用户密码检查永不过期.创建用户时,始终取消选中该检查.
我的代码.
DirectoryEntry user = root.Children.Add(adUserName, "user");
// NOTE(cboivin): Documentation : http://msdn.microsoft.com/en-us/library/aa746340(VS.85).aspx
user.Invoke("SetPassword", new object[] { adUserPassword });
// NOTE(cboivin): Ne pas mettre les clefs dans les ressources, Description, PasswordExpirationDate, AccountExpirationDate
user.Invoke("Put", new object[] { "Description", Nms.SiteAccess.Business.Manager.ActiveDirectory._resources.WindowsPermissionManager.UserCreerAutomatiquement + args.AddDescriptif });
user.Invoke("Put", new object[] { "PasswordExpirationDate", args.PasswordExpiration });
user.Invoke("Put", new object[] { "AccountExpirationDate", args.AccountExpiration });
user.CommitChanges();
Run Code Online (Sandbox Code Playgroud) 在我的.NET 2.0 C#applcation中,我需要确定用户(带密码)是否具有在Active Directory中修改(写入)选项的能力.我希望有一种方法可以使用DirectoryEntry而无需在AD中创建然后删除新对象.
谢谢您的帮助.
我有一些代码可以从 Active Directory 中删除安全组,但在运行时,我收到 COMException 并显示消息“未指定错误”。
这是代码:
public void DeleteGroup(Model.Asset pADSecurityGroup)
{
using(DirectoryEntry ou = new DirectoryEntry(pADSecurityGroup.Organization.ActiveDirectoryMappings.Single().Identifier))
using(DirectoryEntry group = new DirectoryEntry("LDAP://" + pADSecurityGroup.ActiveDirectoryMappings.Single().Identifier))
{
ou.Children.Remove(group);
group.CommitChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是 Windows 事件控制台中的消息:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 8/23/2011 11:29:35 AM
Event time (UTC): 8/23/2011 5:29:35 PM
Event ID: 67e6356c9ff146c7a0d9024350cbb3a0
Event sequence: 79
Event occurrence: 1
Event detail code: 0
Application information:
Application domain: /LM/W3SVC/1/ROOT-2-129585938920392018
Trust level: Full
Application Virtual Path: /
Application Path: …Run Code Online (Sandbox Code Playgroud) 我可以很好地添加用户,但是我无法将其添加到本地组.我收到这个错误: -
无法将成员添加到本地组或从本地组中删除该成员,因为该成员不存在.
这是我正在使用的代码.我做错了什么?它只是本地机器,我绝对有权这样做,而且该组织确实存在.
try
{
using (DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + serverName))
{
DirectoryEntries entries = hostMachineDirectory.Children;
foreach (DirectoryEntry entry in entries)
{
if (entry.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
// Update password
entry.Invoke("SetPassword", password);
entry.CommitChanges();
return true;
}
}
DirectoryEntry obUser = entries.Add(userName, "User");
obUser.Properties["FullName"].Add("Used to allow users to login to Horizon. User created programmatically.");
obUser.Invoke("SetPassword", password);
obUser.Invoke("Put", new object[] {
"UserFlags",
0x10000
});
obUser.CommitChanges();
foreach (string group in groups)
{
DirectoryEntry grp = hostMachineDirectory.Children.Find(group, "group");
if (grp != null) …Run Code Online (Sandbox Code Playgroud) 使用System.DirectoryServices(即DirectoryEntry类)有没有办法将DistinguishedName属性转换为,或获取通常出现在Active Directory用户和计算机管理单元中的规范名称?我意识到我可以使用正则表达式来做到这一点,但我更喜欢更可靠的方法.
例如,我想转换它
CN = Murdock \,James,OU =禁用用户,OU = GOG,DC = contoso,DC =本地
对此
contoso.local/GOG /残疾用户/默多克,詹姆斯