简体   繁体   English

C# 代码在控制台函数中工作,但在 SQL CLR 存储过程中不起作用

[英]C# code works from Console function but does not work in SQL CLR Stored Procedure

PLEASE HELP!!!请帮忙!!! I have a code to get data from AD.我有一个从 AD 获取数据的代码。 This used to work in SQL2014/Visual Studio2013.这曾经在 SQL2014/Visual Studio2013 中工作。 Now, we are migrating to SQL2016.现在,我们正在迁移到 SQL2016。 I tested the code in a Console App and it worked just fine.我在控制台应用程序中测试了代码,它工作得很好。 It just does not work when I create the same code into a SQL CLR Stored Proc using Visual Studio 2017.当我使用 Visual Studio 2017 在 SQL CLR 存储过程中创建相同的代码时,它不起作用。

This is the code in the Console App:这是控制台应用程序中的代码:

        static void Main(string[] args)
        {
            DirectoryEntry ldapConnection;
            DirectorySearcher search;
            SearchResult result;
            DirectoryEntry ldapconnection = null;
            string szJDEID = "";
            string szErrorMessage = "";
            string szNetworkID = "xyz";

            //--- Create and return new LDAP connection with desired settings  
            ldapConnection = new DirectoryEntry("LDAP://DC.company.com:389", "userid", "password");
            ldapConnection.Path = "LDAP://DC=company,DC=com";
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;


            //--- create search object which operates on ldap connection object and set search object to only find the user specified  
            search = new DirectorySearcher(ldapconnection);
            search.Filter = "(&(samaccountname=" + szNetworkID.Trim() + ")(memberOf=CN=JDEdwards Users,OU=Mail Enabled Manual,OU=Groups,OU=Global,DC=company,DC=com))";

            result = search.FindOne();

            if (result != null)
            {
                //--- create an array of properties that we would like and add them to the search object  
                string[] requiredproperties = new string[] { "extensionattribute13" };

                foreach (string property in requiredproperties)
                    search.PropertiesToLoad.Add(property);

                result = search.FindOne();

                if (result != null)
                {
                    foreach (string property in requiredproperties)
                        foreach (object mycollection in result.Properties[property])
                            szJDEID = mycollection.ToString();
                }
            }
            else
            {
                szErrorMessage = "ERROR: This user does not belong to the JDEdwards Users AD Group. Please check with the IT Helpdesk team.";
            }
        }

I get the value of szJDEID as stored in Extension Attribute13.我得到了存储在扩展属性 13 中的 szJDEID 的值。 When I put the same code in a SQL CLR Stored Proc, the Logic always returns the szErrorMessage value.当我将相同的代码放在 SQL CLR 存储过程中时,逻辑总是返回 szErrorMessage 值。

What am I missing?我错过了什么?

Thanks in advance.提前致谢。

Finally.最后。 As you had rightly pointed earlier, the bindDn was incorrect.正如您之前正确指出的那样, bindDn 是不正确的。 The issue is we moved from one DC to another.问题是我们从一个 DC 搬到了另一个 DC。 I used lip.exe to find out the principal - userid@domain.com.我使用 lip.exe 找出主体 - userid@domain.com。 Also, the ldapConnection.Path was not needed anymore as it was incorrect with the new DC.此外,不再需要 ldapConnection.Path,因为它对新 DC 不正确。 So, finally, it is working.所以,最后,它正在工作。 Thanks Clint.谢谢克林特。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储过程在查询生成器中执行时有效,但不适用于C#代码 - Stored procedure works when executed in query builder, but doesn't work from C# code 从C#代码调用存储过程时,出现SQL错误“过程或函数需要参数,但未提供” - Sql error “Procedure or function expects parameter , which was not supplied” when calling a stored procedure from c# code 使用存储过程更新不起作用C#和SQL Server - update using the stored procedure does not work c# and SQL server 存储过程在DB中有效,但在C#代码中无效 - Stored Procedure works in DB but not in C# code SQL CLR存储过程是否会阻止注入? - Does a SQL CLR stored procedure prevent injection? 具有C#抛出错误的CLR存储过程 - CLR Stored Procedure with C# throwing errors 异步 function 不执行 sql CLR 存储过程 - asynchronous function not executing sql CLR stored procedure 从本地计算机使用时,调用SQL存储过程的C#函数可以工作,但是从云中的Azure函数调用时,失败 - C# function that calls a SQL stored procedure works when used from local machine but fails when called from an Azure function in the cloud SQL Server存储过程在查询分析器中执行,但不能在C#中正常运行 - SQL Server stored procedure executes in query analyzer, but does not function properly from C# 从C#代码调用SQL Server存储过程时出错 - Error calling SQL Server stored procedure from C# code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM