OWIN的GetExternalLoginInfoAsync始终返回null [英] OWIN's GetExternalLoginInfoAsync Always Returns null

查看:204
本文介绍了OWIN的GetExternalLoginInfoAsync始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的MVC5 Web应用程序,当我尝试使用谷歌或Facebook的ExternalLoginCallback行动中的AccountController登录被调用,但GetExternalLoginInfoAsync()始终返回null:

I've created a new MVC5 Web Application, and when I try to login with Google or Facebook, the ExternalLoginCallback Action in the AccountController is called, but GetExternalLoginInfoAsync() always returns 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?

推荐答案

要获得OWIN谷歌登录到一个标准的Visual Studio 2013正常工作,ASP.Net MVC5网站,我只好

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

1)设置一个谷歌OpenID帐户在<一个href=\"https://console.developers.google.com/project\">https://console.developers.google.com/project

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

2)设置回调URL来胡说/登入 - 谷歌。对事物的重要注意事项您需要做的:

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

您不需要安装任何东西的重定向URL - 没有航线,控制器操作或Web.Config中的特殊权限。重定向URL始终/登入,谷歌和OWIN处理这一幕后为你。

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.

作为一个例子,如果你的网站是me.com,你可能在谷歌开发者控制台的这3个回调网址:

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

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

第一个包括任何端口号VS给你为你的项目。

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

3)启用了Google+ API 。这是一个疑难杂症的一个隐藏b ****,是问题的问题的根源在这里 - 如果你不这样做,很容易错过的是要求 /帐号/ ExternalLoginCallback 包括&放大器;错误= ACCESS_DENIED ,这是因为谷歌说,没有一个权限要求OWIN为用户的Google+基本概况进行。我不能告诉是谁的错,这是谷歌的或Microsoft的。

3) 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/ExternalLoginCallback includes &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,点击左侧,追捕Google+的API的点击和命中启用。是的,你真的需要做到这一点。 如果你不这样做,你是大清洗。

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.

4)添加客户端Id和ClientSecret谷歌给你的开发者控制台Startup.Auth,但提高的过程中明确使用的OAuth2的code和明确要求,为用户的电子邮件地址:

4) 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);

就是这样。这终于得到了它的工作。

That's it. That finally got it working.

只是想重申一次,有这个答案和问题,如它在哪里OWIN /谷歌是不工作的很多,而且几乎所有的人都错了当前VS2013 / MVC5 / OWIN模板。你并不需要在所有的修改Web.Config中。你并不需要任何创造任何特殊的路线。你不应该试图点/登入,谷歌到不同的位置,或者使用不同的回调URL,你绝对不应该尝试直接比分扳成/帐号/ externallogincallback或externalloginconfirmation,因为那些都是从/ signin-两个独立谷歌和在OWIN /谷歌过程所需的步骤。

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-google to a different place, or use a different callback URL, and you definitely shouldn't attempt to tie it directly to /account/externallogincallback or externalloginconfirmation, because those are both separate from /signin-google and necessary steps in the OWIN/Google process.

这篇关于OWIN的GetExternalLoginInfoAsync始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆