首页 > 解决方案 > .Net 核心社交登录在 nginx 后面不起作用

问题描述

我有一个 asp.net core 3.1 项目。我包括社交登录 nuget 包。

https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Facebook/

https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Google/

https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.MicrosoftAccount/

下面的 Startup.cs

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
.AddFacebook(options =>
{
options.AppId = serverSettings.FacebookLogin.AppId ;
options.AppSecret =serverSettings.FacebookLogin.AppSecret ;
})
.AddMicrosoftAccount(options =>
{
options.ClientId =serverSettings.MicrosoftLogin.ClientId;
options.ClientSecret = serverSettings.MicrosoftLogin.ClientSecret;
})
.AddGoogle(options =>
{
options.ClientId =serverSettings.GoogleLogin.ClientId ;
options.ClientSecret =serverSettings.GoogleLogin.ClientSecret ;
})
.AddCookie(options =>
{
options.LoginPath = "/home/login";
});

下面的控制器

 [Route("signin")]
    public IActionResult SignIn() => View();

    [Route("signin/{provider}")]
    public IActionResult SignIn(string provider, string returnUrl = null) =>
        Challenge(new AuthenticationProperties { RedirectUri = returnUrl ?? "/Auth/ReturnUrl", }, provider);

    [Route("signout")]
    public async Task<IActionResult> SignOut()
    {
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        return RedirectToAction("Index", "Home");
    }

此代码在本地完美运行,但是,当我发送此代码 ubuntu 服务器时。这是行不通的。服务器有nginx。Nginx 作为反向代理工作。将传入请求转发到 localhost:5050

nginx.conf

server {
listen 443 ssl default_server;
location / {
  proxy_pass http://site:5050;
  proxy_redirect http://site:5050/ https://localhost:5050/;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection keep-alive;
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header X-Forwarded-Port 443;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Scheme $scheme;
  proxy_connect_timeout 1;
  proxy_send_timeout 30;
  proxy_read_timeout 30;
}
error_log /var/log/nginx/api_error.log;
access_log /var/log/nginx/api_access.log;
ssl_certificate /etc/xxxxx;
ssl_certificate_key /etc/xxxxx;
}

最后一步是三个平台抛出相同的错误。认证后。当返回我的应用程序错误

Facebook

WWW-Authenticate: OAuth "Facebook Platform" "redirect_uri_mismatch"

谷歌

Body: {
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
 };

我该如何解决这个问题?我认为这是问题所在。将传入请求转发到不同 url 中的 nginx

标签: c#asp.net-corenginx

解决方案


如果我正确理解来自 Google 的错误消息,则说明重定向 URI 不正确。我猜您https://localhost:5000/signin在进行本地开发和测试时配置了重定向 uri 或类似的东西。

现在您正在使用带有 Nginx 的实际远程服务器,您的 URI 不再是localhost. 您需要配置重定向 URI 以匹配指向您的应用程序的服务器地址。如果您已注册并为其配置了 DNS,则您需要使用服务器的实际公共 IP 地址或域名。

您应该能够参考每个特定提供商的文档,了解如何配置它。


推荐阅读