69

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?

7
  • Take a look at: asp.net/mvc/tutorials/mvc-5/…. I followed it yesterday with no problems Nov 5, 2013 at 19:32
  • Did you check whether correct provider is being passed to ChallengeResult of ExternalLogin ActionResult?
    – Santosh
    Nov 11, 2013 at 13:19
  • 2
    Experiencing this same problem. And yes the provider is correct. I click sign in, Im redirected to facebook, I sign in, and then Im redirected to the method... with a null loginInfo. Nov 24, 2013 at 21:36
  • 1
    Experiencing this same problem. It fixes the issue after I restarted the IIS. Is there a proper solution for this issue?
    – Lee
    Jan 5, 2014 at 21:26
  • 7
    I've got the same issue, I forgot to turn on the Google +API
    – sabotero
    Dec 6, 2014 at 21:11

17 Answers 17

106

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

  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't need 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.

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

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

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

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.

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

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

9
  • 12
    Number three did it for me. Sadly, I knew this to be the problem in the past but had forgotten it... and yes, totally agree this is a b**** . Many thanks for the list.
    – roboto1986
    Jul 12, 2015 at 12:08
  • 4
    Thank you, Thank you, Thank You! Worked, yes ENABLE Google+ API, it worked even without "code improvement". Great detailed answer! Jul 28, 2015 at 14:14
  • 1
    If I could upvote you 1000 times, I would. Thank you! Jan 17, 2016 at 5:30
  • 4
    Doesn't work for me. It is actually impossible to create an appID and appSecret without enabling Google+ API. I have enabled it and was able to create the secret and id, but I am still being redirected back to the login page.
    – tutiplain
    May 14, 2016 at 20:53
  • 2
    I have it enabled, but it still doesn't work. I am considering manually doing the Oauth flow, since this seems to be a bug in either Owin or the project templates in Visual Studio.
    – tutiplain
    May 17, 2016 at 15:10
46

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.

5
  • 6
    This immediately solved the problem. Good find, thanks. Jan 4, 2015 at 14:52
  • Now that I look carefully, I see that this step is clearly indicated in the instructions on asp.net. Jan 4, 2015 at 16:51
  • Yes, but I thought it's not obligatory. Anyway the connection between that and the error that is shown is not clear.
    – ronenfe
    Jan 4, 2015 at 16:57
  • Yes, I've seen all kinds of crazy answers but this is the correct one for me. Apr 7, 2015 at 15:09
  • You sir, are a god!
    – Kris
    Mar 18, 2017 at 15:16
20

It seems that Nuget package Microsoft.Owin.Security.Facebook version 3.0.1 no longer works with Facebook Login.

Update this package to the pre-release 3.1.0 version, you can use the following:

Install-Package Microsoft.Owin.Security.Facebook -Pre

7
  • Just came across this out of nowhere today as well and updating to the RC of 3.1.0 worked for me too. Mar 30, 2017 at 1:29
  • Glad to have helped :)
    – Luke
    Apr 6, 2017 at 8:19
  • 2
    Crikey, hope they fix this soon spent all night reconfiguring URLs trying to work out what I'd inadvertently changed since it was working last time I was playing around. Your suggestion worked. Still no updated version later that 3.0.1 to install according to NuGet
    – Richard
    Apr 8, 2017 at 21:35
  • 1
    After trying pretty much everything on stack overflow and wasting about 3 hours, this was the final solution for me. Thanks :)
    – Dan Cook
    Apr 20, 2017 at 22:15
  • 1
    Upgrading to 3.1 fixed my problem. Thanks for saving me many painful hours debugging this!
    – johnnyRose
    Jun 10, 2017 at 3:57
7

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

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

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

Step 3: Enable it! enter image description here

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

4

I got it to work by simply updating all the nugget package in the application and it worked.

1
  • This worked for me too, after trying everything else mentioned here. I updated the OWIN packages from v4.0.0 to v4.0.1 and then it started working. Nov 20, 2019 at 9:15
3

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

2
  • Same here but the problem returned after a while (days). It worked fine for some time then all of a sudden it just stops and a refresh of the app pool solves it again. Very weird.
    – Patrick
    Aug 14, 2015 at 22:14
  • This solved the issue for me. Hopefully it will keep working now. stackoverflow.com/a/20948631/155758
    – Patrick
    Aug 14, 2015 at 22:18
3

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

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

1

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

1

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

Make sure you place...

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

before...

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

I wanted to contribute to this one also. I just recently got this working. I had the problem with the GetExternalLoginInfoAsync returning null but only in production.

After a lot of searching I finally found my answer it was simply a problem with my database. In production I had set the wrong connection string so it would not connect properly but it was basically silent about it. The only thing that happened was GetExternallLoginInfoAsync returned null. So check you database connection string if this happens!

Also on a sidenote, the only thing that was needed to get this working was:

  • Set up a project in the Google console
  • Enable Google+ API
  • Copy your client id and client secret to the Startup.Auth.cs file.

You do not have to enable HTTPS, you do not have to create custom routes. But make sure your database is working properly!

1

For those who are experiencing this problem for Web Api. Other solutions doesnt help AuthenticationManager.GetExternalLoginInfoAsync(); returns always null even google plus api is enabled.

use this custom function to get logininfo. obviously Microsoft has a bug for GetExternalLoginInfoAsync when requesting over web api.

private async Task<ExternalLoginInfo> AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer()
        {
            ExternalLoginInfo loginInfo = null;

            var result = await Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);

            if (result != null && result.Identity != null)
            {
                var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);
                if (idClaim != null)
                {
                    loginInfo = new ExternalLoginInfo()
                    {
                        DefaultUserName = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", ""),
                        Login = new UserLoginInfo(idClaim.Issuer, idClaim.Value)
                    };
                }
            }
            return loginInfo;
        }
2
  • 1
    Thanks this solved my problem Microsoft need to look at this bug May 6, 2019 at 13:38
  • 1
    I have tried this code but on the first attempt return null, but 2nd attempt working fine Oct 31, 2023 at 5:44
0

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.

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

Hope this helps someone else out.

0

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.

0

It is true that you are going to need the Google plus Enabled. The big thing for me was the project URL. Open the properties window (View -> Properties Window) in VS and then right click the project and select properties. In small properties window copy your SSL URL, and then in the larger properties window select the Web tab and paste that URL in the Project URL.

Fixed the issue for me.

See in greater detail: https://learn.microsoft.com/en-us/aspnet/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

0

For me I was migrating and old but working .NET 4.6.1 MVC website to core 1.1. Work stopped before I could get it working, when I picked it back up, I was then migrating to 2.x.

My problem was that the callback from Google was met with a 404 from my site. I thought it was supposed to hit AccountController.ExternalLoginCallback so I added a [Route(...)] to it and sure enough, Google's callback hit the action.

This then hit the null returned in this line (what kind of maniac returns a null?)

var externalLoginInfo = await this.SignInManager.GetExternalLoginInfoAsync();

I reverse engineered it to find under the hood its ultimately getting the handler for ExternalScheme which for my was the cookies handler!

It all seemed wrong and I felt somehow that the middleware was supposed to just intercept the callback URI so I removed my [Route(...)] and the 404 problem came back.

I then found that I need to add this during startup.

applicationBuilder.UseAuthentication();

This solves the 404 but gives another issue.

No authenticationScheme was specified, and there was no DefaultSignInScheme found.

By adding a default scheme here, I resolve the error above.

serviceCollection.AddAuthentication(IdentityConstants.ExternalScheme)
    .AddGoogle(googleOptions => Configuration.Bind("OAuth2:Providers:Google", googleOptions))
    .AddExternalCookie();

Now AccountController.ExternalLoginCallback is invoked again by some magic but I am back to the null return value.

I added this code above the offending line, which is essentially what is happening under the hood (looking at Microsoft's code on GitHub). Interestingly, h is of type CookieAuthenticationHandler and a has all my claims and information from Google inside!

var authHandler = this.HttpContext.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var h = await authHandler.GetHandlerAsync(this.HttpContext, IdentityConstants.ExternalScheme);
var a = await h.AuthenticateAsync();

var externalLoginInfo = await this.SignInManager.GetExternalLoginInfoAsync();

Digging into GitHub and copy pasting internal code its running into my controller, I can see that it's failing to find ClaimTypes.NameIdentifier in my claims, this is the ProviderKey used later.

Hmm....

Concerned I was using old 1.x AccountController code with newer 2.x identity bits I did find some samples that still use this stuff, and some samples that use Razor Pages for it all, too. I'll continue with what I have.

So I'm next going to investigate mapping additional Google user JSON payload items into the claims. I think if my Google account ID (numeric, I guess) was mapped then everything would work.

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-2.2

Final Fix

I finally resolved the issue by adding a "claim action" to pull my Google identifier out of the JSON coming back from Google!

serviceCollection.AddAuthentication(IdentityConstants.ExternalScheme)
    .AddGoogle(googleOptions =>
    {
        Configuration.Bind("OAuth2:Providers:Google", googleOptions);

        googleOptions.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub", "string");
    })
    .AddExternalCookie();

The sub field contains what eventually ends up in the nameidentifier claim and then into the ProviderKey that the AccountController wants.

0

In my case the solution was update Nuget package Microsoft.Owin.Security and Microsoft.Owin.Security.Google

-1

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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.