Asp.net-Mvc

在 MVC5 中使用 OWIN Oauth 的 Google 身份驗證未命中 ExternalLoginCallback 函式

  • April 15, 2014

我目前正在升級我的登錄過程,以便 Google 在他們棄用他們的 OpenID 登錄方法之前使用 OAuth。

到目前為止,我已經確定的步驟是將包 Microsoft.Owin.Security.Google 升級到版本 2.1.0,因為該版本包括在 UseGoogleAuthentication 方法中包含選項的能力。

我嘗試在連結中使用 Alex Wheat 的解決方案: Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

Startup.Auth.cs 中的程式碼(還包括 Facebook 身份驗證)來自以下內容:

   var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
       {
           AppId = "MYAPPID",
           AppSecret = "MYSECRET"
       };
       facebookAuthenticationOptions.Scope.Add("email");
       app.UseFacebookAuthentication(facebookAuthenticationOptions);

       app.UseGoogleAuthentication();

對此:

var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
       {
           AppId = "MYAPPID",
           AppSecret = "MYSECRET"
       };
       facebookAuthenticationOptions.Scope.Add("email");
       app.UseFacebookAuthentication(facebookAuthenticationOptions);


       var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
       {
           ClientId = "MYCLIENTID",
           ClientSecret = "MYSECRET",
           CallbackPath = new PathString("/en/Account/ExternalLoginCallback"),
           Provider = new GoogleOAuth2AuthenticationProvider()
           {

           }
       };

       app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

在我向 Google 身份驗證添加選項後,我的應用程序不允許為 google 或 facebook 呼叫 ExternalLoginCallback 操作(沒有更改 facebook 程式碼,但問題仍然會影響它)。

在前端,點擊外部登錄按鈕後,頁面將我重定向到下面的連結並返回一個空白螢幕

https……/en/Account/ExternalLoginCallback#__=_ (= 符號之前實際上只有一個下劃線,如果我有它,那麼語法會刪除它,因為它出現在我的地址欄上)。

臉書和

https……/en/Account/ExternalLoginCallback

對於Google。它沒有像往常一樣點擊下面的控制器方法(我試圖在這個函式中放置調試斷點,當有Google身份驗證選項時它永遠不會停止。

   // GET: /Account/ExternalLoginCallback
   [AllowAnonymous]
   public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
   {

如果我從 Google 身份驗證中刪除身份驗證選項,它只會恢復到舊的 OpenID 登錄並再次正常工作。

我在這裡錯過了一些簡單的東西嗎?還是在導致問題的 Owin.Security.Google 庫中發生了一些不好的事情?

只試試這個

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
       {
           ClientId = "MYCLIENTID",
           ClientSecret = "MYSECRET",
       };
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

這對我有用

為簡單起見,我使用帶有身份驗證的預設 ASP.NET MVC 5 模板,但希望可以針對不同的案例進行修改。

啟動驗證.cs

不要自定義重定向路徑。無論如何,它都會被 /signin-google 取代,而我試圖解決這個問題會導致“靜默”(不在調試器中)內部伺服器 500 錯誤。

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
   ClientId = "whatevs.apps.googleusercontent.com",
   ClientSecret = "whatevs_secrut",
   Provider = new GoogleOAuth2AuthenticationProvider()
});

確保在您的> >部分中將<http://whatever.com/signin-google>添加到https://console.developers.google.com/ 。APIs & auth``Credentials``Redirect URIs

路由配置.cs

將路由添加到您的路由的永久重定向控制器操作。永久重定向是這裡唯一足夠的東西。僅僅直接指向回調 URL 是不夠的。

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute(
       name: "Google API Sign-in",
       url: "signin-google",
       defaults: new { controller = "Account", action = "ExternalLoginCallbackRedirect" }
   );

   routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

AccountController.cs

永久重定向到內置回調方法,你應該沒問題。

[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
   return RedirectPermanent("/Account/ExternalLoginCallback");
}

已經在 GitHub 上發布了一個模板項目供參考: https ://github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication

引用自:https://stackoverflow.com/questions/23072786