Search code examples
asp.netasp.net-coreoauth-2.0asp.net-identity

SignInManager.ExternalLoginSignInAsync fails to log in user


In the context of ASP.NET Identity in ASP.NET Core and OAuth, I have the following snippet in a login callback:

var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
    return RedirectToAction(nameof(Login));
}

var result = await _signInManager.ExternalLoginSignInAsync(
    info.LoginProvider,
    info.ProviderKey,
    isPersistent: true);

if (result.Succeeded)
{
    // ...

For some reason that I'm unable to determine, result always comes back with status Failed, even though I do log into the external provider correctly (evidently, since a refresh of the external page suddenly shows me as logged on).

The only information I have except that the login attempt failed, is that it is not because any of IsLockedOut, RequiresTwoFactor or IsNotAllowed, since those flags are also false on the result object.

How do I obtain more information about what went wrong?


Update:

Following suggestions by @Dmitry, I've enabled quite aggressive logging, but I still fail to find anything valuable. The only thing I get in my logs between the successful fetch of info and the subsequent failed login attempt, is the following from my db context:

2017-07-04T22:45:14.2847287+02:00 0HL633KNRMVO7 [INF] Executed DbCommand (1ms) [Parameters=[@__get_Item_0='?' (Size = 450), @__get_Item_1='?' (Size = 450)], CommandType='Text', CommandTimeout='30'] SELECT TOP(1) [e].[LoginProvider], [e].[ProviderKey], [e].[ProviderDisplayName], [e].[UserId] FROM [AspNetUserLogins] AS [e] WHERE ([e].[LoginProvider] = @__get_Item_0) AND ([e].[ProviderKey] = @__get_Item_1) (6438bdd5)

Executing that in MSSQL Management Studio I get an empty result set, which I suspect might be a problem, but I have no idea why, or how to fix it. Any help is much appreciated.


Solution

  • You are missing a call to UserManager.AddLoginAsync (https://msdn.microsoft.com/en-us/library/dn497534(v=vs.108).aspx), which will populate the AspNetUserLogins table.