MVC5 ExternalLoginCallback上的Google OAuth?错误=访问被拒绝

MVC5 ExternalLoginCallback上的Google OAuth?错误=访问被拒绝,oauth,asp.net-mvc-5,google-oauth,Oauth,Asp.net Mvc 5,Google Oauth,我已经建立了我的谷歌OAuth 我已经将代码添加到Startup.Auth.cs中 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() { // LRC ClientId = "xxxxxxxxx", ClientSecret = "xxxxx" //CallbackPath = new PathString("/signin-googl

我已经建立了我的谷歌OAuth

我已经将代码添加到Startup.Auth.cs中

 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        // LRC
        ClientId = "xxxxxxxxx",
        ClientSecret = "xxxxx"
        //CallbackPath = new PathString("/signin-google")
    });
但在我选择了一个谷歌账号登录后,它再次将我重定向到登录页面

我通过Chrome检查了网络,发现访问被拒绝

我想不出来


更新 现在我做了另外一件事:

  • 我在帐户控制器上添加了注释([RequireHttps])
  • 我为我的项目启用了SSL
  • 我更新了url并将Google控制台中的url重新定向为https
  • 尝试登录谷歌,在我选择我的谷歌帐户后,它返回了相同的访问被拒绝


    如果谷歌的回复能够提供更详细的信息就更好了。

    我在使用最新的ASP.Net MVC模板时遇到了同样的问题,并选择了“个人帐户”

    解决方案是在googledeveloper控制台中为我的项目启用Google+API


    我找到了答案(向下滚动到“对Google OAuth 2.0的更改…”)。

    我也遇到了这个问题。启用Google+API后,问题还没有解决。事实证明,我还没有在我的谷歌API控制台中设置“授权JavaScript源代码”。所以我设置了,问题就解决了。

    同样的错误也发生在我的脸书提供商身上

    结果证明,解决方案非常简单

    事实证明,Facebook对其graph API进行了“强制升级” 2017年3月27日从2.2版到2.3版

    为了记录在案,我使用了以下内容:

    • 在没有https的iisexpress中
    在Facebook中,我为测试应用程序配置了以下设置:

    此外,如果您使用的是示例模板,则返回的
    error
    参数未被使用,这可能会产生误导。您应该将
    string error
    添加到
    ExternalLoginCallback

        [AllowAnonymous]
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl, string error)
        {
            if (error != null)
            {
                return View("Error");
            }
    
    [AllowAnonymous]
    公共异步任务ExternalLoginCallback(字符串返回URL,字符串错误)
    {
    if(错误!=null)
    {
    返回视图(“错误”);
    }
    
    这很可能是因为您尚未在开发人员控制台中启用Google+API

    因此,当您的帐户试图获取有关谷歌帐户的详细信息时,它会显示
    access\u denied


    只需转到开发人员控制台并启用Google+API,我就遇到了同样的问题。我让Google+API处于活动状态并设置了JavaScript提供程序。结果发现,我的Microsoft.Owin 3.1版本太旧了。我已经更新了每一个以Microsoft.Owin.(无论什么)为名的金块,它开始正常工作(4.1版)


    希望能有所帮助!

    以上解决方案对我都不起作用。事实证明,在我的案例中,我使用了
    Google OAuth Playerd
    ,并在我的Google客户ID和机密凭据的授权重定向URI部分添加了此url

    当我把它取下来重试时,它工作得很好

    PS:我不得不重新设置我也修改过的
    OAuth操场
    设置

    编辑

    另一个问题是,当用户被触发时,我的代码抛出了一个
    异常
    。结果是一个空引用,导致返回
    访问被拒绝
    状态

    GoogleOAuth2AuthenticationOptions googleOptions = new GoogleOAuth2AuthenticationOptions()
                {
                    ClientId = "xxxxx.apps.googleusercontent.com",
                    ClientSecret = "XXXX",
                    Provider = new GoogleOAuth2AuthenticationProvider()
                    {
                        OnAuthenticated = (context) =>
                        {
                            try
                            {
                                TokenHelper tokenHelper = new TokenHelper();
    
                                // Any exception here will result in 'loginInfo == null' in AccountController.ExternalLoginCallback.
                                // Be sure to add exception handling here in case of production code.
                                context.Identity.AddClaim(new Claim(tokenHelper.AccessToken, context.AccessToken)); // From This line and onwards. tokenHelper's properties were null.
    
                                // For clarity, we don't check most values for null but RefreshToken is another kind of thing. It's usually
                                // not set unless we specially request it. Typically, you receive the refresh token only on the initial request,
                                // store it permanently and reuse it when you need to refresh the access token.
                                if (context.RefreshToken != null)
                                {
                                    context.Identity.AddClaim(new Claim(tokenHelper.RefreshToken, context.RefreshToken));
                                }
    
                                // We want to use the e-mail account of the external identity (for which we doing OAuth). For that we save
                                // the external identity's e-mail address separately as it can be different from the main e-mail address
                                // of the current user. 
                                context.Identity.AddClaim(new Claim(tokenHelper.Email, context.Email));
                                context.Identity.AddClaim(new Claim(tokenHelper.Name, context.Name));
    
                                context.Identity.AddClaim(new Claim(tokenHelper.IssuedOn, DateTime.Now.ToString()));
                                context.Identity.AddClaim(new Claim(tokenHelper.ExpiresIn,
                                    ((long)context.ExpiresIn.Value.TotalSeconds).ToString()));
    
                                return Task.FromResult(0);
                            }
                            catch (Exception ex)
                            {
    
                                throw;
                            }
    
                        },
                    },
                    AccessType = "offline",
                    UserInformationEndpoint= "https://www.googleapis.com/oauth2/v2/userinfo"
                };
    

    默认的Google身份验证不再有效,您可以通过NuGet添加更新的Owin.Security.Provider.Google包或查找它

    谢谢M,没错。在我的情况下,我还发现我需要更多的信息。因此在启用Google+API并注释掉一些代码行以减少所需信息后,程序你必须在项目属性上启用ssl,并在你的站点上使用https,否则你将看到访问被拒绝的问题Shi Franva,你能告诉我如何解决这个问题吗?我使用https,将我的url设置为授权的javascript源,启用GoogleAPI+,但我总是被拒绝访问,并且我得到302状态码。该链接现在是一个死链接k、 不知道那里是否有更多的信息。感谢您添加一个屏幕截图来解释,这非常有用。这不是唯一的原因。它在我这边启用,但仍然会出现错误。