Closed Mimetis closed 6 years ago
Ok, got it.
You have to save the tokens, since the Identity.External
cookie seems to be deleted after the authentication (do not hesitate to correct me if I'm wrong)
So the deal is to add the tokens after Post back authentication
if (result.Succeeded)
{
result = await _userManager.AddLoginAsync(user, info);
if (result.Succeeded)
{
var props = new AuthenticationProperties();
props.IsPersistent = true;
props.ExpiresUtc = DateTime.UtcNow.AddDays(5);
props.StoreTokens(info.AuthenticationTokens);
await _signInManager.SignInAsync(user, props, info.LoginProvider);
// Add this to add token to datastore
// Update the token
await _signInManager.UpdateExternalAuthenticationTokensAsync(info);
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
return LocalRedirect(Url.GetLocalUrl(returnUrl));
}
}
This is not an easy path to get something that seems to me a basic scenario in my opinion ? What do you think ?
Other bonus question : How do you handle the token expiration ?
Hi all; I can't figured out how to retrieve the
access_token
once authenticated from within theMicrosoftAccount
provider.Well, to be honest, I should be able to retrieve it if it's stored in a data store using the
_signinManager
instance.Storing the token in my datastore
Following this issue https://github.com/aspnet/Identity/issues/1452
Making a SQL call on every requests does not seems correct to me.
Getting the value from a cookie / claim / datastore ?
I tried to set the tokens in a cookie, or a claims, but actually, I didn't find anyway to make it work...
Here is my extract code:
The
microsoftOptions.SaveTokens = true;
allows me to retrieve the tokens on call back and then saved tokens using theSigninManager
.It's working, but not want I want, actually...
During the
OnCreatingTicket
event, I tried several things. My last attempt was to try to set an expired time on the cookie generation:Finally, after sucessfully log in, I try to retrieve my tokens, with several methods, but none of them are working, yet :)
I tried to change some properties like the default schema, trying
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
orservices.AddAuthentication(MicrosoftAccountDefaults.AuthenticationScheme)
but nothing works actually.Do you have any idea ?