gpt4 book ai didi

c# - 如何在使用托管 ODP.NET 时从 C# 查询 LDAP 以解析 Oracle TNS 主机名?

转载 作者:太空狗 更新时间:2023-10-29 21:34:41 26 4
gpt4 key购买 nike

除了我之前的 Question ,我设法用 help from the Oracle forums 回答了自己,我现在还有另一个问题,它是之前的问题(提供背景)。

我希望直接从我的 C# 代码查询 LDAP,以执行 Oracle TNS 主机名的 LDAP 查找,以便获取连接字符串。这通常存储在 tnsnames.ora 中,我的组织使用 LDAP(通过 ldap.ora)从使用 Active Directory 的 LDAP 服务器解析主机名。

但是,我在我的 C# 应用程序中使用 ODP.NET, Managed Driver Beta (Oracle.ManagedDataAccess.dll),它不支持 LDAP,正如我之前提到的 release notes 指向的 Oracle forum reply 中提到的那样。这就是为什么我希望直接从 C# 查询 LDAP。

我找到了一种使用 DirectoryEntryDirectorySearcher 来执行此 here 的方法,但我不知道将什么作为参数放入 DirectorySearcher .我可以访问以下格式的 ldap.ora:

# LDAP.ORA Configuration
# Generated by Oracle configuration tools.
DEFAULT_ADMIN_CONTEXT = "dc=xx,dc=mycompany,dc=com"
DIRECTORY_SERVERS = (ldap_server1.mycompany.com:389:636,ldap_server2.mycompany.com:389:636, ...) DIRECTORY_SERVER_TYPE = OID

但是,我如何将其映射到在我的 C# 代码中设置 LDAP 查询?

最佳答案

除了我在 accepted Answer 中的第二条评论之外,这是执行 LDAP 查找的代码,它改进了我找到的原始版本 here 。它还处理 ldap.ora 文件中的服务器列表,其中包括多个分隔的端口号。

private static string ResolveServiceNameLdap(string serviceName)
{
string tnsAdminPath = Path.Combine(@"C:\Apps\oracle\network\admin", "ldap.ora");
string connectionString = string.Empty;

// ldap.ora can contain many LDAP servers
IEnumerable<string> directoryServers = null;

if (File.Exists(tnsAdminPath))
{
string defaultAdminContext = string.Empty;

using (var sr = File.OpenText(tnsAdminPath))
{
string line;

while ((line = sr.ReadLine()) != null)
{
// Ignore commetns
if (line.StartsWith("#"))
{
continue;
}

// Ignore empty lines
if (line == string.Empty)
{
continue;
}

// If line starts with DEFAULT_ADMIN_CONTEXT then get its value
if (line.StartsWith("DEFAULT_ADMIN_CONTEXT"))
{
defaultAdminContext = line.Substring(line.IndexOf('=') + 1).Trim(new[] {'\"', ' '});
}

// If line starts with DIRECTORY_SERVERS then get its value
if (line.StartsWith("DIRECTORY_SERVERS"))
{
string[] serversPorts = line.Substring(line.IndexOf('=') + 1).Trim(new[] {'(', ')', ' '}).Split(',');
directoryServers = serversPorts.SelectMany(x =>
{
// If the server includes multiple port numbers, this needs to be handled
string[] serverPorts = x.Split(':');
if (serverPorts.Count() > 1)
{
return serverPorts.Skip(1).Select(y => string.Format("{0}:{1}", serverPorts.First(), y));
}

return new[] {x};
});
}
}
}

// Iterate through each LDAP server, and try to connect
foreach (string directoryServer in directoryServers)
{
// Try to connect to LDAP server with using default admin contact
try
{
var directoryEntry = new DirectoryEntry("LDAP://" + directoryServer + "/" + defaultAdminContext, null, null, AuthenticationTypes.Anonymous);
var directorySearcher = new DirectorySearcher(directoryEntry, "(&(objectclass=orclNetService)(cn=" + serviceName + "))", new[] { "orclnetdescstring" }, SearchScope.Subtree);

SearchResult searchResult = directorySearcher.FindOne();

var value = searchResult.Properties["orclnetdescstring"][0] as byte[];

if (value != null)
{
connectionString = Encoding.Default.GetString(value);
}

// If the connection was successful, then not necessary to try other LDAP servers
break;
}
catch
{
// If the connection to LDAP server not successful, try to connect to the next LDAP server
continue;
}
}

// If casting was not successful, or not found any TNS value, then result is an error message
if (string.IsNullOrEmpty(connectionString))
{
connectionString = "TNS value not found in LDAP";
}
}
else
{
// If ldap.ora doesn't exist, then return error message
connectionString = "ldap.ora not found";
}

return connectionString;
}

关于c# - 如何在使用托管 ODP.NET 时从 C# 查询 LDAP 以解析 Oracle TNS 主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14566632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com