admin管理员组

文章数量:1410723

I followed this guide .0 which lead me here and I've managed to get Google auth working. I have access to the GoogleCredential object via the IGoogleAuthProvider.

Now I'm trying to persist the credentials so that I can run a background task at a later point on behalf of the user. Along the way I found this .0#save-the-access-token which provides a way to get the access token but that's of limited use for me without the refresh token.

Can anyone point me in the right direction?

I followed this guide https://learn.microsoft/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-9.0 which lead me here https://developers.google/api-client-library/dotnet/guide/aaa_oauth and I've managed to get Google auth working. I have access to the GoogleCredential object via the IGoogleAuthProvider.

Now I'm trying to persist the credentials so that I can run a background task at a later point on behalf of the user. Along the way I found this https://learn.microsoft/en-us/aspnet/core/security/authentication/social/social-without-identity?view=aspnetcore-9.0#save-the-access-token which provides a way to get the access token but that's of limited use for me without the refresh token.

Can anyone point me in the right direction?

Share Improve this question edited Mar 11 at 4:31 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 11 at 2:34 Alejandro HuertaAlejandro Huerta 1,1442 gold badges17 silver badges38 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If you want to get the refresh token for the google auth, you should set the `googleOptions.AccessType = "offline"; ` which request the refresh token.

More details, you could refer to below codes:

builder.Services.AddAuthentication(options =>
{
     options.DefaultChallengeScheme = "Google"; // Use the scheme name you've configured for Google authentication
}).AddGoogle("Google",googleOptions =>
{
    googleOptions.ClientId = " ";
    googleOptions.ClientSecret = " ";
    googleOptions.SaveTokens = true;
 
    googleOptions.Scope.Add("https://www.googleapis/auth/userinfo.profile");
    googleOptions.Scope.Add("https://www.googleapis/auth/userinfo.email");
    googleOptions.AccessType = "offline"; // Request a refresh token

    googleOptions.Events = new OAuthEvents
    {
        OnCreatingTicket = context =>
       {

           var accessToken = context.AccessToken;

           var re = context.RefreshToken;

           return Task.CompletedTask;

       }
    };
});



Result:

While Zhang's answer would likely work fine using that library, I'm using AspNetCore3 due to being pointed in that direction via the current tutorial and guides. I did some digging into the AspNetCore3 source code and managed to put together a solution following what it does to manage its AccessTokens.

Here's a link to the source file and the relevant line already marked https://github/googleapis/google-api-dotnet-client/blob/main/Src/Support/Google.Apis.Auth.AspNetCore3/GoogleAuthProvider.cs#L62 With that it's fairly straightforward to persist what you need.

本文标签: How do I persist Google credentials in ASPNET CoreStack Overflow