gpt4 book ai didi

c# - 如何使用 msTSProfilePath、msTSHomeDirectory 等扩展 C# DirectoryServices UserContext

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:29 25 4
gpt4 key购买 nike

我需要读/写 ActiveDirectory 用户对象终端服务属性。我试过这个:

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA");

using (context)
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic");
if (user != null)
{
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
entry.Properties["msTSProfilePath"].Value = "";
entry.Properties["msTSHomeDirectory"].Value = "";
entry.Properties["msTSHomeDrive"].Value = "";
entry.CommitChanges();
}

}

我试过这个:

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA");

using (context)
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic");
if (user != null)
{
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
entry.InvokeSet("msTSProfilePath", "");
entry.InvokeSet("msTSHomeDirectory", "");
entry.InvokeSet("msTSHomeDrive", "");
entry.CommitChanges();
}

}

但没有任何作用。

我还尝试使用以下属性名称:

  • 终端服务配置文件路径
  • 终端服务主目录
  • TerminalServicesHomeDrive

但运气不好。任何帮助将不胜感激!

谢谢,沃金

最佳答案

如果您使用的是 .NET 3.5 及更高版本并使用 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间,则可以轻松扩展现有的 UserPrincipal 类以获得更高级的属性,如 Manager 等。

在这里阅读所有相关内容:

基本上,您只需定义一个基于 UserPrincipal 的派生类,然后定义您想要的其他属性:

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
// Implement the constructor using the base class constructor.
public UserPrincipalEx(PrincipalContext context) : base(context)
{ }

// Implement the constructor with initialization parameters.
public UserPrincipalEx(PrincipalContext context,
string samAccountName,
string password,
bool enabled) : base(context, samAccountName, password, enabled)
{}

// Create the "TermSrvProfilePath" property.
[DirectoryProperty("msTSProfilePath")]
public string TermSrvProfilePath
{
get
{
if (ExtensionGet("msTSProfilePath").Length != 1)
return string.Empty;

return (string)ExtensionGet("msTSProfilePath")[0];
}
set { ExtensionSet("msTSProfilePath", value); }
}
}

现在,您可以在代码中使用 UserPrincipalEx 的“扩展”版本:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// Search the directory for the new object.
UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

// you can easily access the TermSrvProfilePath now
string path = inetPerson.TermSrvProfilePath;
}

同样,您也可以添加您要查找的其他属性。

关于c# - 如何使用 msTSProfilePath、msTSHomeDirectory 等扩展 C# DirectoryServices UserContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967791/

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