在安全和非安全的DirectoryServices.AccountManagement.PrincipalContext之间切换

时间:2017-12-29 09:52:21

标签: c# asp.net directoryservices

我正在努力解决以下问题,这就是我现在所拥有的,但它正在发挥作用。

 public PrincipalContext getPrincipalContext(bool secured)
 {
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain,
        secured ?  DomainName + ":636" : DomainName,
        Container,
        secured ? ContextOptions.SecureSocketLayer | ContextOptions.Negotiate : ContextOptions.SimpleBind,
        userName,
        password);
    return oPrincipalContext;
 }

想要做这样的事情

secured ?  PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain,
            DomainName + ":636",
            Container,
            ContextOptions.SecureSocketLayer | ContextOptions.Negotiate 
            userName,
            password); 
        :
          PrincipalContext oPrincipalContext = new  PrincipalContext(ContextType.Domain,
            DomainName,
            Container,
            ContextOptions.SimpleBind);
    return oPrincipalContext;

我认为这是可能的,但无法编译。有人能以正确的方式帮助我吗?

1 个答案:

答案 0 :(得分:0)

可能是这样的:

 PrincipalContext oPrincipalContext = secured ? 
         new PrincipalContext(ContextType.Domain,
            DomainName + ":636",
            Container,
            ContextOptions.SecureSocketLayer | ContextOptions.Negotiate 
            userName,
            password); 
        :
          new PrincipalContext(ContextType.Domain,
            DomainName,
            Container,
            ContextOptions.SimpleBind );

或者您为什么不使用if, else