web-dev-qa-db-ja.com

OVCを使用したGoogle認証Oauth MVC5でExternalLoginCallback関数にヒットしない

現在、Googleのログインプロセスをアップグレードして、OpenIDログインメソッドを廃止する前にOAuthを使用しています。

これまでに特定したステップは、Microsoft.Owin.Security.Googleパッケージをバージョン2.1.0にアップグレードしたことです。このバージョンにはUseGoogleAuthenticationメソッドにオプションを含める機能が含まれています。

リンクでアレックス・ウィートのソリューションを使用しようとしました: 外部認証プロバイダーでMVC5フレームワークOAuth/OWin IDプロバイダーからExtraDataを取得

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#__ = _(=記号の前には実際にはアンダースコアが1つしかありません。SO構文は、アドレスバーに表示されているとおりに削除すると削除されます) 。

facebook用と

https ....../en/Account/ExternalLoginCallback

グーグル用。通常のように以下のコントローラーメソッドにはヒットしません(この関数内にデバッグブレークポイントを配置しようとしましたが、Google認証オプションがあるときに停止しません。

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

Google認証から認証オプションを削除すると、古いOpenIDログインに戻り、正常に機能するようになります。

ここで簡単なものが欠けていますか?または、Owin.Security.Google Library内で問題を引き起こしている悪いことがありますか?

24
Brad Baskin

これだけやってみて

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

これは私のために働いた

15
Suhas Joshi

簡単にするために、Identity Authenticationで既定のASP.NET MVC 5テンプレートを使用していますが、これをさまざまなユースケースに合わせて変更できることを願っています。

StartupAuth.cs

リダイレクトパスをカスタマイズしないでください。とにかく/ signin-googleに置き換えられ、それを回避しようとすると、「サイレント」(デバッガーではない)内部サーバー500エラーが発生しました。

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

必ず http://whatever.com/signin-googlehttps://console.developers.google.com/ に追加してくださいAPIs & auth> Credentials> Redirect URIs セクション。

RouteConfig.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

20

デベロッパーコンソールでGoogle+ APIも有効にしていることを確認してください。クライアントとシークレットを取得した後の追加手順です

13
Aaron Sherman

@Ronenがコメントで述べたように、このリンクはGoogleの問題を解決するはずですOAuth:

http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for- 3-0-0-rc-release.aspx

また、NuGetからOWINパッケージを更新します。これは私のコードがどのように見え、うまく機能するかです:

       var googleOptions = new GoogleOAuth2AuthenticationOptions ()
       {
           ClientId = "xxxxxxxxxx",
           ClientSecret = "xxxxxxxxxx",
           CallbackPath = new PathString("/signin-google")
       };
       googleOptions.Scope.Add("email");

       app.UseGoogleAuthentication(googleOptions);
3
Rick