【发布时间】:2015-01-19 14:45:59
【问题描述】:
所以我正在使用 UserPrincipal 类创建一个 AD 用户,但我无法通过它设置允许拨入设置。
如何启用设置?
提前致谢!
【问题讨论】:
标签: c# .net active-directory
所以我正在使用 UserPrincipal 类创建一个 AD 用户,但我无法通过它设置允许拨入设置。
如何启用设置?
提前致谢!
【问题讨论】:
标签: c# .net active-directory
您不能在 UserPrincipal 类中设置所有属性。这可以在 DirectoryEntry 类的帮助下给出一个想法:
//Get the Directory entry of your Userprincipal
DirectoryEntry directoryEntry = [YOUR_USERPRINCIPAL_OBJECT].GetUnderlyingObject() as DirectoryEntry;
//Set Allow Dialin attribute
directoryEntry.Properties["msNPAllowDialin"].Value = false;
//Save changes
directoryEntry.CommitChanges();
希望对你有帮助
【讨论】:
如果您愿意,可以使用 UserPrincipal 使用 user principal extension 读取和写入 DialIn 选项
您可以像这样创建扩展用户主体
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtension : UserPrincipal
{
public UserPrincipalExtension(PrincipalContext context) : base(context) { }
public UserPrincipalExtension(PrincipalContext context, string samAccountName, string password, bool isEnabled)
: base(context, samAccountName, password, isEnabled)
{
}
[DirectoryProperty("msNPAllowDialin")]
public bool? DialIn
{
get
{
if (ExtensionGet("msNPAllowDialin").Length != 1)
return null;
return (bool?)ExtensionGet("msNPAllowDialin")[0];
}
set { this.ExtensionSet("msNPAllowDialin", value); }
}
}
然后就可以设置属性了
//this code will create a new user on AD
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "SharePoint", "OU=Employees,DC=SharePoint,DC=Com"))
{
using (UserPrincipalExtension up = new UserPrincipalExtension(context))
{
//Set all others properties
up.DialIn = true; //true = Allow access, false = Deny access, null = control access through policy
up.Save();
}
}
//this code will update a user
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "SharePoint", "OU=Employees,DC=SharePoint,DC=Com"))
{
using (UserPrincipalExtension up = UserPrincipalExtension.FindByIdentity(context, samAccountName))
{
//Set all others properties
up.DialIn = true; //true = Allow access, false = Deny access, null = control access through policy
up.Save();
}
}
【讨论】: