How to request offline_access from Google using .AddOpenIdConnect() in ASP.NET Core?

I know Microsoft provides a Google-specific OIDC package (Microsoft.AspNetCore.Authentication.Google) which takes an option in .AddGoogle() to specify AccessType that can be set to offline.

But can this be done using the standard ASP.NET Core OIDC package Microsoft.AspNetCore.Authentication.OpenIdConnect and .AddOpenIdConnect()?

With Microsoft account we can simply request the offline_access scope and it works perfectly. But it does not work with Google and results in an invalid_scope error.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Figured this out. Google uses the access_type parameter for offline access request instead of scope. So we can handle the OnRedirectToIdentityProvider event in OpenIdConnectOptions to add this parameter:

 options.Events.OnRedirectToIdentityProvider = context =>
 {
   context.ProtocolMessage.SetParameter("access_type", "offline");
   return Task.CompletedTask;
 };


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x