2017-08-28 60 views
1

我正在開發一個網站ASP.NET Core 2Nginx。我想添加Google身份驗證。更改RedirectUri,Nginx和ASP.NEt核心

服務器是Linux,運行Nginx,啓用了SSL。 ASP.NET在http://localhost:5000/上運行,Nginx使用反向代理進行訪問。

問題是,當我嘗試訪問Google時,URL模式爲http,因爲ASP.NET在http中運行。我收到錯誤說「錯誤:redirect_uri_mismatch」,因爲https是在Google端定義的。

我試過OnRedirectToAuthorizationEndpoint事件,並改變了RedirectUri但沒有奏效。

我也嘗試將CallbackPath更改爲完整的網址,但據說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); 
     }; 
    }); 

回答

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;