用于连接Oracle Directory Server Enterprise Edition的C#脚本

时间:2014-09-18 15:37:09

标签: c# oracle ldap

我有一个c#脚本我试图用来连接 Oracle Directory Server Enterprise Edition 到目前为止,我的成功很少。 我收到未知错误(0x80005000)错误消息 有人能告诉我我做错了什么。我一直在研究网络,大多数在线主板都说这个错误信息是因为路径中的LDAP需要是大写字母。 你可以看到我已经做到了但仍然没有运气。

以下是我的代码

 private static readonly string PATH = "LDAP://LDAPDEV.example.com:389/o=example.com";
 private static readonly string USERNAME = uid=SERVICE_USR,ou=ApplicationIDs,o=example.com";
 private static readonly string PASSWORD = "test1234";  


      string DN = "";

        // connect to determine proper distinguishedname
        DirectoryEntry Entry = new DirectoryEntry(Path, USERNAME, PASSWORD, AuthenticationTypes.None);

        try
        {

            // Bind to the native AdsObject to force authentication.
            Object obj = Entry.NativeObject;

            DirectorySearcher Search = new DirectorySearcher(Entry);
            Search.ReferralChasing = ReferralChasingOption.All

        }
        catch (Exception ex)
        {
            throw new Exception("Error looking up distinguishedname. ", ex);
        }
        finally
        {
            anonymousEntry.Close();
        }

        return DN;


    }
enter code here

string sDomain="LDAPDEV.example.com:389"; 
string sDefaultOU = @"o=example.com"; 
string sServiceUser = @"uid=user,ou=ApplicationIDs,o=example.com"; 
string sServicePassword = "password"; 

try 
 {
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ontextOptions.SimpleBind, sServiceUser,sServicePassword); 

 } 
 catch (Exception ex) 
 {
          ex.Message; 
 }

1 个答案:

答案 0 :(得分:0)

通过使用PrincipalContext查看此工作示例并使用您的域名值替换值,可以使用以下方法获取某些信息。

 // if you are doing this via web application if not you can replace
 // the Request/ServerVariables["LOGON_USER"] 
 // with Environment.UserName; this will return your user name like msmith for example so var userName = Environvironment.UserName; 
 var userName = Request.ServerVariables["LOGON_USER"].Split(new string[] {"\\"}, StringSplitOptions.RemoveEmptyEntries);
 var pc = new PrincipalContext(ContextType.Domain,"yourdomain.com",null, null);
 var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);

这基本上只需要查看电子邮件地址SamAccountName等信息。使用调试器检查pc变量和userFind变量中可用的所有属性值