我在IIS 8上的Windows 2012 R2环境中设置ASP.NET CORE 1.0.1。应用程序连接到SQL数据库,并在application.json文件中有一个连接字符串(信息已更改):
"ConnectionStrings": {
"Database": "Data Source=qadb;Initial Catalog=companydb;Trusted_Connection=True"
},
在IIS中,我已经设置了应用程序,并且应用程序池已设置为以特定用户(例如company \ sqluser)运行。我验证了公司\ sqluser对数据库的读/写访问权限。应用程序池的高级设置如下所示:
.NET CLR Version: No Managed Code
Enable 32-bit Application: False
Managed Pipeline Mode: Integrated
Identity: company\sqluser
当我运行该应用程序时,我收到以下SQL错误消息:
System.Data.SqlClient.SqlException: Login failed for user ''.
我想让应用程序连接到DB作为公司\ sqluser,但由于某种原因,它似乎不能这样做。我还尝试将应用程序池作为我的个人用户帐户运行,该帐户具有SQL DB的管理员权限,但仍然会收到错误。
我错过了某种配置吗?
编辑:我的web.config文件如下:
<system.web>
<authentication mode="Windows"/>
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\CompanyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
<rewrite>
<rules>
<rule name="HTTP to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" /><conditions><add input="{HTTPS}" pattern="off" />
</conditions><action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
因为我不是应用程序的开发人员,所以我没有意识到开发人员在代码中覆盖了连接字符串而不是使用appsettings.json文件。缺少的是以下内容:
Trusted_Connection=True;
一旦我删除了覆盖连接字符串的代码并确保在appsettings中Trusted_Connection设置为True,该应用程序就可以工作了。
在连接字符串中尝试使用Integrated Security=true
而不是Trusted_Connection=True
。
Integrated Security=true
是System.Data.SqlClient的语法,而Trusted_Connection=true
是ODBC的语法。
见https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=netcore-1.0和https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-string-syntax
您可以尝试设置Windows身份验证。在IIS中,确保添加并启用了Windows身份验证模块。
此外,需要确保应用程序池在域帐户而非本地帐户下运行。
在ASP.net中,我们通常这样做:
<system.web>
<authentication mode="Windows"/>
</system.web>
在Asp.net核心中,这种方式通常有效: