Hello everyone I have used this code for User Auth by Google Auth and retrieved the token But I have faced an issue this token has expired only 5 min . I want this token is not expire.
Loading
Hello everyone I have used this code for User Auth by Google Auth and retrieved the token But I have faced an issue this token has expired only 5 min . I want this token is not expire.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 16, 2024, 2:53 AM EDT
To handle the issue of the token expiring after 5 minutes, you need to implement a mechanism to refresh the token before it expires. This can be achieved by using the Google API client library for .NET.
Here's an example of how you can modify your code to refresh the token:
public async Task OnGetLinkLoginCallbackAsync(SMTPProviderDetails sMTPProviderDetails) { // Existing code... // Get the refresh token var refreshToken = info.AuthenticationTokens.FirstOrDefault(t => t.Name == "refresh_token")?.Value; // Create a new instance of the Google API client var client = new Google.Apis.Auth.OAuth2.GoogleCredential { AccessToken = info.AuthenticationTokens.FirstOrDefault(t => t.Name == "access_token")?.Value, RefreshToken = refreshToken }.CreateScoped(new[] { "https://www.googleapis.com/auth/userinfo.email" }); // Check if the access token is about to expire if (client.Token.ExpiresInSeconds <= 300) // 300 seconds = 5 minutes { // Refresh the access token await client.RefreshTokenAsync(CancellationToken.None); // Update the existing record with the new access token existingRecord.SMTPPassword = client.Token.AccessToken; existingRecord.UpdateOn = DateTime.UtcNow; } // Existing code... await _context.SaveChangesAsync(); return RedirectToPage(); }we retrieve the refresh token from the authentication tokens obtained from Google. We then create a new instance of the Google API client and set the access token and refresh token. We check if the access token is about to expire (within 5 minutes) and if so, we refresh the token using the
RefreshTokenAsyncmethod. Finally, we update the existing record with the new access token.By implementing this token refresh mechanism, you can ensure that the token does not expire and the user can continue to use the Google Authentication without interruption.
Sourabh DhimanRank #2,987 · 483 reputationPosted Apr 16, 2024, 1:49 AM EDT
This is my code
public async Task OnPostLoginAsync(string provider) { await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); var redirectUrl = Url.Page(".SmtpSetting", pageHandler: "LinkLoginCallback"); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User)); return new ChallengeResult(provider, properties); } public async Task OnGetLinkLoginCallbackAsync(SMTPProviderDetails sMTPProviderDetails) { var user = await _userManager.GetUserAsync(User); if (user == null) { return NotFound($"Unable to load user with ID 'user.Id'."); } var info = await _inManager.GetExternalLoginInfoAsync(user.Id.ToString()); var emailClaim = info.Principal.Claims.FirstOrDefault(c => c.Type == "emailaddress"); if (info == null) { throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id.ToString()}'."); } var existingRecord = await _context.SMTPProviderDetails.FirstOrDefaultAsync(x => x.UserId == user.Id); if (existingRecord != null) { // Update existing record existingRecord.SMTPPassword = info.AuthenticationTokens.FirstOrDefault(t => t.Name == "access_token")?.Value; existingRecord.UpdateOn = DateTime.UtcNow; if (emailClaim != null) { existingRecord.UserEmail = emailClaim.Value; } } else { // Insert new record sMTPProviderDetails.CreatedOn = DateTime.UtcNow; if (emailClaim != null) { sMTPProviderDetails.UserEmail = emailClaim.Value; } sMTPProviderDetails.UserId = Convert.ToInt32(user.Id); sMTPProviderDetails.UserName = user.FirstName; sMTPProviderDetails.SMTPPassword = info.AuthenticationTokens.FirstOrDefault(t => t.Name == "access_token")?.Value; _context.Add(sMTPProviderDetails); } await _context.SaveChangesAsync(); return RedirectToPage(); }