c# - 更改 RedirectUri、Nginx 和 ASP.NEt Core

标签 c# asp.net asp.net-mvc nginx

我正在使用 ASP.NET Core 2Nginx 开发一个网站。我想添加 Google 身份验证。

服务器是 Linux,运行 Nginx,启用 SSL。 ASP.NET运行在http://localhost:5000/上,Nginx使用反向代理访问。

问题是,当我尝试访问 Google 时,URL 模式是 http,因为 ASP.NET 在 http 中运行。我收到错误提示“错误:redirect_uri_mismatch”,因为 https 是在 Google 端定义的。

我尝试了 OnRedirectToAuthorizationEndpoint 事件并更改了 RedirectUri 但没有成功。

我也尝试过将 CallbackPath 更改为完整的 URL,但据说 URL 必须以 / 开头;它需要相对路径。

我使用的包是:Microsoft.AspNetCore.Authentication.Google (2.0.0)

有什么建议吗?

服务代码是:

services.AddAuthentication()
    .AddGoogle(options =>
    {
        options.ClientId = Configuration["Authentication:Google:ClientId"];
        options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
        options.CallbackPath = "/Login/GoogleCallback";
        options.Events.OnRedirectToAuthorizationEndpoint += context =>
        {
            context.Properties.RedirectUri = context.Properties.RedirectUri.Replace("http://", "https://");
            return Task.FromResult(0);
        };
    });

最佳答案

将这些添加到配置文件 /etc/nginx/sites-available/default 后,它开始工作。

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;

关于c# - 更改 RedirectUri、Nginx 和 ASP.NEt Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45923776/

相关文章:

c# - Asp .Net 文件缓存

c# - 从 .NET Core 项目引用可移植类库

asp.net - 使用 NAnt 中的 ASP 编译器构建 ASP .Net MVC 应用程序

c# - 如何计算 C# 中的类属性?

c# - JSON.NET 和数组使用 LINQ

c# - C# 从蓝牙设备获取数据

c# - 如何将信息从一个类传递到另一个类

asp.net - 如何发布使用 Enterprise Library 进行日志记录的 Web 应用程序?

c# - 简单的评估表不显示在表中

javascript - 从 JQuery Ajax 处理 FileResult