C# OWIN 的 GetExternalLoginInfoAsync 总是返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19775321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:51:28  来源:igfitidea点击:

OWIN's GetExternalLoginInfoAsync Always Returns null

c#.netasp.net-mvcowingoogle-openid

提问by VineetYadav

I've created a new MVC5 Web Application, and when I try to login with Google or Facebook, the ExternalLoginCallbackAction in the AccountControlleris called, but GetExternalLoginInfoAsync()always returns null:

我创建了一个新的 MVC5 Web 应用程序,当我尝试使用 Google 或 Facebook 登录时,调用了其中的ExternalLoginCallbackAction AccountController,但GetExternalLoginInfoAsync()始终返回 null:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
    return RedirectToAction("Login");
}

Because it's always null, it just redirects back to the login page and the process starts over. How can I fix this?

因为它始终为空,所以它只是重定向回登录页面并重新开始该过程。我怎样才能解决这个问题?

回答by Omri

I know it's silly, but after a long struggle, restarting IIS solved the issue for me.

我知道这很愚蠢,但经过长时间的斗争,重新启动 IIS 为我解决了这个问题。

回答by tony

I did the following to get it working.

我做了以下工作来让它工作。

Logon to the developer portal, locate your application and do the following.

登录开发人员门户,找到您的应用程序并执行以下操作。

App details > App centered Listed Platforms > Select Yes for website

应用详情 > 以应用为中心的上市平台 > 为网站选择是

回答by Ronen Festinger

OK, I found out why it's null. You have to enable Google + API in the Google console. Also make sure the secret key is not concatenated with a space at the end after you paste it to your code. Why can't they return a normal error? I don't know.

好的,我找到了为什么它为空。您必须在 Google 控制台中启用 Google + API。还要确保在将密钥粘贴到代码后,密钥没有在末尾连接一个空格。为什么他们不能返回一个正常的错误?我不知道。

回答by Chris Moschini

To get OWIN Google login to work properly on a standard Visual Studio 2013, ASP.Net MVC5 site, I had to:

为了让 OWIN Google 登录在标准的 Visual Studio 2013、ASP.Net MVC5 站点上正常工作,我必须:

  1. Setup a Google OpenId account at https://console.developers.google.com/project

  2. Set the callback URL there to blah/signin-google.
    Important notes on things you don'tneed to do:

    • You don't need to use HTTPS for Google to redirect back; you can even redirect back to plain http://localhost, no problem.

    • You don't need to setup anything for the redirect URL - no routes, Controller Actions or special permissions in Web.Config. The redirect URL is always /signin-google and OWIN handles this behind the scenes for you.

  1. https://console.developers.google.com/project设置 Google OpenId 帐户

  2. 将那里的回调 URL 设置为blah/signin-google
    关于您不需要做的事情的重要说明:

    • 您不需要使用 HTTPS 来让 Google 重定向回来;你甚至可以重定向回普通的http://localhost,没问题。

    • 您不需要为重定向 URL 设置任何内容 - Web.Config 中没有路由、控制器操作或特殊权限。重定向 URL 始终为 /signin-google,OWIN 会在幕后为您处理此问题。

As an example, if your site was me.com, you might have these 3 callback URLs in the Google Developer Console:

例如,如果您的站点是 me.com,则您可能在 Google Developer Console 中有以下 3 个回调 URL:

http://localhost:53859/signin-google
http://test.me.com/signin-google
https://me.com/signin-google

The first one including whatever port number VS gave you for your project.

第一个包括 VS 为您的项目提供的任何端口号。

  1. Enable the Google+ API. This is one hidden b**** of a gotcha and is the root cause of the problem in the question here - if you don't do this, it's easy to miss that the Request to /account/ExternalLoginCallbackincludes &error=access_denied, and that's because Google said no to a permissions request OWIN made for the user's Google+ basic profile. I can't tell whose fault this is, Google's or Microsoft's.
  1. 启用 Google+ API。这是一个隐藏的 b****,是这里问题中问题的根本原因 - 如果你不这样做,很容易错过 Request to /account/ExternalLoginCallbackincludes &error=access_denied,那是因为谷歌说不OWIN 为用户的 Google+ 基本个人资料提出的权限请求。我不知道这是谁的错,谷歌的还是微软的。

To enable the Google+ API in the Developers Console, click APIs on the left, hunt for Google+, click that and hit Enable. Yes you really do need to do that. You're hosed if you don't do that.

要在开发人员控制台中启用 Google+ API,请单击左侧的 API,搜索 Google+,单击它并点击启用。是的,您确实需要这样做。如果你不这样做,你就会被灌输。

  1. Add the ClientId and ClientSecret Google gave you in the Developers Console to Startup.Auth, but improve the code in the process to explicitly use OAuth2, and explicitly ask for the user's email address:

    var google = new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = "123abc.apps.googleusercontent.com",
        ClientSecret = "456xyz",
        Provider = new GoogleOAuth2AuthenticationProvider()
    };
    google.Scope.Add("email");
    app.UseGoogleAuthentication(google);
    
  1. 将开发者控制台中谷歌给你的ClientId和ClientSecret添加到Startup.Auth中,但是改进了过程中的代码,显式使用OAuth2,并显式询问用户的邮箱地址:

    var google = new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = "123abc.apps.googleusercontent.com",
        ClientSecret = "456xyz",
        Provider = new GoogleOAuth2AuthenticationProvider()
    };
    google.Scope.Add("email");
    app.UseGoogleAuthentication(google);
    

That's it. That finally got it working.

就是这样。这终于让它工作了。

Just want to reiterate one more time, there are a LOT of answers about this and issues like it where OWIN/Google isn't working, and nearly all of them are wrong for the current VS2013/MVC5/OWIN template.
You don't need to modify Web.Config at all.
You don't need to create any special Routes whatsoever.
You should not attempt to point /signin-googleto a different place, or use a different callback URL, and you definitely shouldn't attempt to tie it directly to /account/externallogincallbackor externalloginconfirmation, because those are both separate from /signin-googleand necessary steps in the OWIN/Google process.

只想再重申一次,有很多关于这个问题的答案以及 OWIN/Google 无法正常工作的类似问题,而且对于当前的 VS2013/MVC5/OWIN 模板,几乎所有答案都是错误的。
您根本不需要修改 Web.Config。
您不需要创建任何特殊的路由。
您不应尝试指向/signin-google不同的位置,或使用不同的回调 URL,并且绝对不应尝试将其直接绑定到/account/externallogincallbackexternalloginconfirmation,因为这些/signin-google步骤与 OWIN/Google 流程中的步骤是分开的,也是必要的。

回答by rman

All of the other answers didn't solve this for me, so if your in the same boat then make sure your registration controller action has the RequireHttps attribute:

所有其他答案都没有为我解决这个问题,因此如果您在同一条船上,请确保您的注册控制器操作具有 RequireHttps 属性:

    // GET: /Account/LoginRegister
    [AllowAnonymous]
    [RequireHttps]
    public ActionResult LoginRegister()
    {
        return View(new RegisterLoginViewModel());
    }

回答by Hugh Proctor

This solved my problem:

这解决了我的问题:

Enable the Google+ API. This is a gotcha and is the root cause of the problem in the question here - if you don't do this, it's easy to miss that the Request to /account/ExternalLoginCallbackincludes &error=access_denied, and that's because Google said no to a permissions request OWIN made for the user's Google+ basic profile. I can't tell whose fault this is, Google's or Microsoft's.

启用 Google+ API。这是一个陷阱,是这里问题中问题的根本原因 - 如果您不这样做,很容易错过 Request to /account/ExternalLoginCallbackincludes &error=access_denied,这是因为 Google 拒绝了 OWIN 为用户提出的权限请求Google+ 基本个人资料。我不知道这是谁的错,谷歌的还是微软的。

To enable the Google+ API in the Developers Console, click APIs on the left, hunt for Google+, click that and hit Enable.

要在开发人员控制台中启用 Google+ API,请单击左侧的 API,搜索 Google+,单击它并点击启用。

回答by JJones

Although the answers above are all good, in my instance none of these worked - I'd checked and double checked the Google settings and agree with Chris Moschini that there's a lot of misleading info.

尽管上面的答案都很好,但在我的例子中,这些都没有奏效 - 我检查并仔细检查了 Google 设置,并同意 Chris Moschini 的观点,即存在很多误导性信息。

For me it was a 'doh moment when I realised that my Out of Process state service was not started! No errors (as a login was the first thing I was attempting after a reboot where the state service is set to manual start-up on the machine) just a Null from GetExternalLoginInfoAsync

对我来说,当我意识到我的进程外 状态服务没有启动时,真是太棒了!没有错误(因为登录是我在重新启动后尝试的第一件事,其中状态服务设置为在机器上手动启动)只是来自GetExternalLoginInfoAsync

Hope this helps someone else out.

希望这可以帮助其他人。

回答by Graham Walker

After much searching and head scratching as well as following numerous red herring answers here on Stackoverflow I eventually went through all my options on my Google dev console and discovered a little blue [Enable] button on the Google+API overview page. I clicked this and hey presto it worked. Forget all the baloney you read about callback url and route configs, OWIN overrides the google default /signin-google redirect uri in any case and sends you back to ExternalLoginCallback. Just stick with the default implementation all will be good so long as you enable your Google+API.

经过大量搜索和摸索,并在 Stackoverflow 上遵循了无数的红鲱鱼答案后,我最终在我的 Google 开发控制台上浏览了所有选项,并在 Google+API 概述页面上发现了一个蓝色的 [启用] 按钮。我点击了这个,嘿,它很快就起作用了。忘记您阅读的有关回调 url 和路由配置的所有废话,OWIN 在任何情况下都会覆盖谷歌默认的 /signin-google 重定向 uri,并将您发送回 ExternalLoginCallback。只要坚持使用默认实现,只要您启用 Google+API,一切都会好起来的。

回答by Hossein Narimani Rad

As others correctly mentioned, most of the time that's because you do not have permission to the Google+ API so here is how to get permission for a project in Google API Manager to Google+ API

正如其他人正确提到的那样,大多数情况下这是因为您没有使用 Google+ API 的权限,所以这里是如何在 Google API Manager 中为 Google+ API 获取项目权限

Step 1.Select You Project from the top combobox and go to Dashboard > Enable API enter image description here

步骤 1.从顶部组合框中选择您的项目,然后转到仪表板 > 启用 API 在此处输入图片说明

Step 2:Search for Google plus and select it enter image description here

第 2 步:搜索 Google plus 并选择它 在此处输入图片说明

Step 3:Enable it! enter image description here

第 3 步:启用它! 在此处输入图片说明

if you return to dashboard for that project you can see the list of enabled API's for that project at the bottom enter image description here

如果您返回该项目的仪表板,您可以在底部看到该项目的已启用 API 列表 在此处输入图片说明

回答by Hagge

I ran in to this issue today and it turned out that I defined the remote cookie after I assigned the providers.

我今天遇到了这个问题,结果是我在分配提供程序后定义了远程 cookie。

Make sure you place...

确保你放置...

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

before...

前...

app.UseFacebookAuthentication(
                   appId: "",
                   appSecret: "");