asp.netasp.net-coreasp.net-identityopenid-connectgoogle-openid

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.


Solution

  • 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;
     };