I am using ASP.NET Core Identity with social login providers in my ASP.NET Core 3.1 API. I can login with the configured providers successfully. After logging in, I have a controller that needs access to the access_token that was provided by the social authentication provider to query for additional data (Facebook in this case).
Does ASP.NET Core Identity store the access token from the social login provider anywhere by default or is that my responsibility to write it to a cookie or session in the ExternalLogin callback page of the scaffolded identity code after calling GetExternalLoginInfoAsync()?
It seems GetExternalLoginInfoAsync() only returns information in the ExternalLogin callback as calling this same method from my controller always returns NULL. So maybe the cookie is removed after successfully logging in. This seems a bit strange as I would expect it to be able to return the related record stored stored in the AspNetUserLogin table as the user is signed in when calling that method from the controller.
I've also tried using the following code to store all the tokens, but can't seem to find them anywhere. Where does it store them?
services.AddAuthentication()
.AddFacebook(fb =>
{
fb.AppId = Configuration["FacebookAppId"];
fb.AppSecret = Configuration["FacebookAppSecret"];
fb.SaveTokens = true;
fb.Scope.Add("user_birthday");
fb.Scope.Add("user_friends");
fb.Events.OnCreatingTicket = ctx =>
{
List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
tokens.Add(new AuthenticationToken()
{
Name = "TicketCreated",
Value = DateTime.UtcNow.ToString()
});
ctx.Properties.StoreTokens(tokens);
return Task.CompletedTask;
};
})