ASP.. NET Web API: URL调用没有返回



我有一个ASP。. NET Web API,我需要通过https://api.mydomain.com到达。我用。net创建了这个API,发布并部署了内容到我的服务器(自包含的应用程序)。

我在我的服务器上创建了一个托管在IIS上的网站,并在端口443绑定上添加了api.mydomain.com,因为它是一个安全的API。

当我尝试通过我的前端网站调用API时,我得到一个500错误。

它执行飞行前检查,返回204,然后下一次调用失败500错误

下面是Fiddler classic的结果

OPTIONS https://api.mydomain.com/api/auth HTTP/1.1
Host: api.mydomain.com
Connection: keep-alive
Accept: */*
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: https://dashboard.mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
Sec-Fetch-Dest: empty
Referer: https://dashboard.mydomain.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

POST https://api.mydomain.com/api/auth HTTP/1.1
Host: api.mydomain.com
Connection: keep-alive
Content-Length: 90
sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
Accept: application/json, text/plain, */*
Content-Type: application/json
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
sec-ch-ua-platform: "Windows"
Origin: https://dashboard.mydomain.com
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://dashboard.mydomain.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

我的appsettings.Production.json是这样的:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"dbConnection": "Server=myserver\SQLEXPRESS;Database=MyDD;User Id=gg;pwd=gg"
},
"Jwt": {
"Key": "test",
"Issuer": "JWTAuthenticationServer",
"Audience": "JWTServicePostmanClient",
"Subject": "JWTServiceAccessToken"
},
"ApiUrl": "http://api.mydomain.com"
}

我已经尝试了这些数据库连接字符串,但没有运气

"dbConnection": "Data Source=0.0.0.0,1433;Initial Catalog=MyDD;User Id=gg;Password=gg"
"dbConnection": "Server=myserver\SQLEXPRESS;Database=MyDD;Trusted_Connection=True;MultipleActiveResultSets=true

我在应用程序中添加了Serilog以获得更多有用的错误,但日志中没有显示任何内容

网站日志如下图

2023-04-01 01:08:09 0.0.0.0 OPTIONS /api/auth - 443 - 0.0.0.0 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/111.0.0.0+Safari/537.36 https://dashboard.mydomain.com/ 204 0 0 14
2023-04-01 01:08:09 0.0.0.0 POST /api/auth - 443 - 0.0.0.0 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/111.0.0.0+Safari/537.36 https://dashboard.mydomain.com/ 500 0 0 37

我已经尝试使用邮差打电话到终点,但即使用户名或密码错误,也有500错误。

SQL日志提示没有登录尝试,所以我不知道如何调试这个错误。

如有任何帮助,不胜感激。

所以我添加了更多的查找,我可以识别问题,但我看不出这是一个问题。

我正在连接到一个sql express数据,这些字符串

"dbConnection": "Server=VMname\SQLEXPRESS;Database=database;Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True;"

在我的seri日志中,我看到这些错误

2023-04-03 18:50:23.460 +00:00 [INF] HTTP OPTIONS /api/auth responded 204 in 0.6836 ms
2023-04-03 18:50:38.139 +00:00 [ERR] An error occurred using the connection to database 'data' on server 'VMNameSQLEXPRESS'.
2023-04-03 18:50:38.141 +00:00 [ERR] An exception occurred while iterating over the results of a query for context type 'API.Models.v1.DatabaseContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SNI_PN11, error: 26 - Error Locating Server/Instance Specified)

,但我已经切和改变字符串,但没有。这是正确的名称,它接受远程(我可以通过IP从本地机器访问它)

我甚至用过这个字符串,但是没有乐趣。

"dbConnection": "Server=.SQLEXPRESS;Database=database;Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True;"

我已经尝试使用服务器IP地址和端口1433,但仍然没有。有人能帮忙吗?

好的,所以我已经修复了这个问题。这里是如何,我得到了一个500错误,但不知道问题在哪里,做了很多诊断来确定它是什么。

  1. 我添加了serilog以获得更多的错误日志。它确定它有一个SQl连接问题
  2. 将连接字符串更改为
"dbConnection": "Data Source=0.0.0.0,1433;Initial Catalog=MyDD;User Id=gg;Password=gg;Trusted_Connection=True"
  1. 这很重要!当你改变连接字符串时,确保你回收应用程序池来接受更改。