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

1

5

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.

Rhondarhondda answered 26/1, 2021 at 23:51 Comment(0)
R
9

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;
 };
Rhondarhondda answered 27/1, 2021 at 0:14 Comment(1)
This is such an obscure way to do it, this answer needs to have a few more 1000's upvotes.Papen

© 2022 - 2024 — McMap. All rights reserved.