已启用 C# Active Directory 用户设置拨入

C# Active Directory User Set Dial-In enabled

所以我正在使用 UserPrincipal Class 创建一个 AD 用户,但我无法通过它设置允许拨入设置。

如何启用设置?

提前致谢!

您不能在 UserPrincipal class 中设置所有属性。这可以给出如何在 DirectoryEntry class:

的帮助下完成它的想法
//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

的拨入选项

您可以像这样创建扩展用户主体

        [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();
    }
}