pedrommuller
pedrommuller

Reputation: 16066

GetExternalLoginInfoAsync returns null dotnet core 2.0

I'm trying to setup Facebook authentication with dot-net core 2.0, but in my ExternalLoginCallbackAsync method, I'm always getting null as a response I have followed the documentation and so far this is what I've done:

in my ConfigureServices in the startup file:

services.AddAuthentication(
            options => options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme);

services.AddAuthentication().AddFacebook(
    f => {
        f.AppId = Configuration["facebook-app-id"];
        f.AppSecret = Configuration["facebook-app-secret"];
});

in my login controller:

public IActionResult ExternalLogin(string provider)
        {

            var authProperties = new AuthenticationProperties
            {
                RedirectUri = Url.Action("ExternalLoginCallbackAsync", "Login")                
            };
            return Challenge(authProperties,provider);
        }

in my ExternalLoginCallbackAsync method when I do

var info = await _signInManager.GetExternalLoginInfoAsync();

any hint why am I always getting null?

thanks

Upvotes: 4

Views: 3719

Answers (3)

Saad Shaikh
Saad Shaikh

Reputation: 179

I think .net introduced a helper method in SignInManager.

[HttpGet("/auth/signin/{provider}")]
public async Task<ActionResult<UserInfo>> SignInExternal(string provider, [FromQuery] string redirectUrl)
{
    return Challenge(
        properties: signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl), 
        authenticationSchemes: provider
    );
}

Upvotes: 0

AQutaishat
AQutaishat

Reputation: 31

In my case, I had to enable SSL, once I enabled SSL it returned info correctly, to enable SSL, right click on the project --> properties --> Debug --> check (Enable SSL) in web server strings section. I'm using core 3.0

Upvotes: 0

pedrommuller
pedrommuller

Reputation: 16066

I looked at the SignInManager code as Lasse Vabe Rolstad suggested and for me, a key was missing in the auth properties so I had to add it manually like this:

 var authProperties = new AuthenticationProperties
 {
     RedirectUri = Url.Action("ExternalLoginCallbackAsync", "Login"),
     Items = { new KeyValuePair<string, string>("LoginProvider",provider) }
 };

Upvotes: 6

Related Questions