Asp.net core MailKit.Security.SslHandshakeException:主机名与服务器';s的SSL证书。asp.net核心5,nginx
当我尝试在生产容器化应用程序上通过google和MailKit libriry发送消息时,我有一个例外Asp.net core MailKit.Security.SslHandshakeException:主机名与服务器';s的SSL证书。asp.net核心5,nginx,asp.net-core,nginx,ssl,reverse-proxy,mailkit,Asp.net Core,Nginx,Ssl,Reverse Proxy,Mailkit,当我尝试在生产容器化应用程序上通过google和MailKit libriry发送消息时,我有一个例外 MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection. The host name did not match the name given in the server's SSL certificate. 我使用asp.
MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection. The host name did not match the name given in the server's SSL certificate.
我使用asp.NETCore5和Kestrel。Nginx我的反向代理。
当我使用邮递员获取数据时,SSL工作正常。但当我尝试发送邮件时,会出现异常。
在没有nginx服务器代理的开发环境中,代理可以正常工作
这是我的nginx.conf文件:
server {
client_max_body_size 6M;
listen 80;
server_name myhost.com www.myhost.com;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
client_max_body_size 6M;
listen 443 ssl;
server_name myhost.com www.myhost.com;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_types text/plain application/javascript text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype;
#SSL code
ssl_certificate /etc/letsencrypt/live/myhost.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myhost.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
#headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
# location /map {
# proxy_pass http://client;
# }
location /admin {
proxy_pass http://client-admin;
}
location /api {
proxy_pass http://api:5000;
}
}
这是我的Program.cs文件:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace MonumentsMap
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => {
logging.ClearProviders();
logging.AddConsole();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureKestrel(conf => {
conf.Limits.MaxRequestBodySize = 6_000_000;
});
});
}
}
docker编写文件
version: "3.8"
services:
api:
container_name: api
build: ./Api
depends_on:
- db
restart: unless-stopped
environment:
ASPNETCORE_URLS: http://+:5000
volumes:
- ./Images:/app/Images
db:
container_name: db
image: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: api_db
volumes:
- ./postgres-data:/var/lib/postgresql/data:rw
client:
container_name: client
build: ./client
depends_on:
- api
restart: unless-stopped
client-admin:
container_name: client-admin
build: ./client-admin
depends_on:
- api
restart: unless-stopped
stdin_open: true
nginx:
image: nginx:stable-alpine
container_name: docker-nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf.prod:/etc/nginx/conf.d/nginx.conf
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
depends_on:
- client
- client-admin
- certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
您可以通过以下方式禁用SSL/TLS:
smtp.Connect(_mailSettings.Host, _mailSettings.Port, **MailKit.Security.SecureSocketOptions.None**);
请参见,您可以通过以下方式禁用SSL/TLS
smtp.Connect(_mailSettings.Host, _mailSettings.Port, **MailKit.Security.SecureSocketOptions.None**);
请参见您能为您的答案添加一个解释吗?你能给你的答案加个解释吗?
version: "3.8"
services:
api:
container_name: api
build: ./Api
depends_on:
- db
restart: unless-stopped
environment:
ASPNETCORE_URLS: http://+:5000
volumes:
- ./Images:/app/Images
db:
container_name: db
image: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: api_db
volumes:
- ./postgres-data:/var/lib/postgresql/data:rw
client:
container_name: client
build: ./client
depends_on:
- api
restart: unless-stopped
client-admin:
container_name: client-admin
build: ./client-admin
depends_on:
- api
restart: unless-stopped
stdin_open: true
nginx:
image: nginx:stable-alpine
container_name: docker-nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf.prod:/etc/nginx/conf.d/nginx.conf
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
depends_on:
- client
- client-admin
- certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
smtp.Connect(_mailSettings.Host, _mailSettings.Port, **MailKit.Security.SecureSocketOptions.None**);