【问题标题】:C# Active Directory User Set Dial-In enabledC# Active Directory 用户设置拨入已启用
【发布时间】:2015-01-19 14:45:59
【问题描述】:

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

如何启用设置?

提前致谢!

【问题讨论】:

    标签: c# .net active-directory


    【解决方案1】:

    您不能在 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();
    

    希望对你有帮助

    【讨论】:

    • @Claudio P 我可以使用 serviceAccount 以这种方式编辑属性吗?如果我创建一个新的 DE,我会重载使用用户名/密码创建一个条目来授权访问。 GetUnderlyingObject() 是否有任何可能性,或者我应该使用 new DE() 代替吗?
    【解决方案2】:

    如果您愿意,可以使用 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      相关资源
      最近更新 更多