无法在运行时通过IIS托管的ASP.NET Core应用程序设置ODBC源

时间:2019-01-07 07:21:10

标签: asp.net iis asp.net-core odbc iis-8

我有一个ASP.NET Core应用程序,旨在使用Full .NET Framework 4.6,其中包含部分代码,其中我在Startup.cs中调用的ODBC中创建了DSN:

...
 public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureSqlContext(Configuration);
            services.CrearDSN(Configuration);
...

在ServiceExtensions.cs中:

private const short ODBC_ADD_DSN = 1; //' Add user data source 
       private const short ODBC_CONFIG_DSN = 2; //' Configure (edit) data source 
       source 
        private const int vbAPINull = 0;

    public static void CrearDSN(this IServiceCollection services, IConfiguration config)
            {
                //DSNBaseOperativa = Replace(Left((My.Settings.Servidor & My.Settings.BaseDatos & Application.ProductName), 32), "\", "") 'El nombre tiene como maximo 32 caracteres.

            //IOptions<ConnectionODBC> odbcsettings;

            string Driver = "SQL Server"; 
            int ReturnValue;
            string Attributes;

            //string name = config["SQLconnection:Server"] + config["SQLconnection:Database"] + "InteliBS";
            string name = config["SQLConnection:Server"] + config["SQLConnection:Database"] + "InteliBS";
            string DSNBaseOperativa =  name.Length > 32 ? name.Substring(0, 32).Replace( "\\", "") : name.Replace("\\", ""); //'El nombre tiene como maximo 32 caracteres.

            Attributes = "SERVER=" + config["SQLConnection:Server"] + "\0";
            Attributes += "DESCRIPTION=Temp DSN" + "\0";
            Attributes += "DSN=" + DSNBaseOperativa + "\0";
            Attributes += "DATABASE=" + config["SQLConnection:Database"] + "\0";
            //'To show dialog, use Form1.Hwnd instead of vbAPINull. 
            ReturnValue = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, Driver, Attributes); //TODO: Poder regresar el valor en debug o pararlo en caso 0
            if (ReturnValue != 0){
                System.Diagnostics.Debug.WriteLine("Se ha cargado un DSN de ODBC: " + DSNBaseOperativa, "INFO");
                config["ConnectionODBC:DSN"] = DSNBaseOperativa;
                config["ConnectionODBC:Description"] = "Temp DSN";
                config["ConnectionODBC:Server"] = config["SQLConnection:Server"];
                config["ConnectionODBC:Database"] = config["SQLConnection:Database"];
                config["ConnectionODBC:Userid"] = config["SQLConnection:Userid"];
                config["ConnectionODBC:Password"] = config["SQLConnection:Password"];

                services.Configure<ConnectionODBC>(config.GetSection("ConnectionODBC"));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("No se pudo crear un DSN de ODBC", "INFO");
            }

        }

// Use DllImport to import the Win32 SQLConfigDataSource function.
        [DllImport("ODBCCP32.DLL", CharSet = CharSet.Ansi)]
        private static extern int SQLConfigDataSource(int hwndParent, int ByValfRequest, string lpszDriver, string lpszAttributes);

也许您想知道为什么我要配置ODBC而不是常规SQL连接字符串。原因是旧版库要求在应用程序的某些部分中我将不作解释。一切都照常使用Entity Framework。

当我使用IISExpress从Visual Studio 2017运行我的应用程序时,它的工作原理就像是一种魅力。如果不存在,它将创建一个没有问题的ODBC用户源。

但是,当我尝试将其托管在Windows Server 2012 R2上的IIS服务器上时,它不起作用。 IIS8是否有限制措施可以防止这种情况发生?

1 个答案:

答案 0 :(得分:-1)

运行IIS的用户没有必要的权限。

从应用程序池中为此应用程序选择其他用户解决了该问题。

相关问题