'Using Xamarin Essentials WebAuthenticator to get Google Id_token
Im using the Xamarin Essentials as a part of a Xamarin Forms application i'm developing. I have integrated it in to my aspnet.core backend, and it works fine.
However im getting an accesstoken returned when i sign in with google and facebook, but in case of the google login, i need the id_token and not the accesstoken.
I've looked at the 'AddJwtBearer' method on the 'AddAuthentication' part in startup, but i have not been able to make it work when i use the 'AuthenticateAsync' method in controller.
How would one accomplish this? The backend is based on code from this sample: https://github.com/xamarin/Essentials/blob/develop/Samples/Sample.Server.WebAuthenticator/Controllers/MobileAuthController.cs
Solution 1:[1]
try to get it from here:
var auth = await Request.HttpContext.AuthenticateAsync(scheme);
var idtoken = auth.Properties.GetTokenValue("id_token");
or
string refreshToken = await HttpContext.GetTokenAsync("id_token");
Check this class
public class WebAuth
{
private const string AuthenticationUrl ="https://myapp.com/mobileauth/";
private string _accessToken = "";
private bool _isAuthenticated = false;
public string AuthToken
{
get => _accessToken;
set => SetProperty(ref _accessToken, value);
}
async Task OnAuthenticate(string scheme)
{
try
{
scheme = "Facebook";
WebAuthenticatorResult result = null;
var authUrl = new Uri(AuthenticationUrl + scheme);
var callbackUrl = new Uri("myapp://");
result = await WebAuthenticator.AuthenticateAsync(authUrl, callbackUrl);
AuthToken = string.Empty;
if (result.Properties.TryGetValue("name", out var name) && !string.IsNullOrEmpty(name))
{
AuthToken += $"Name: {name}{Environment.NewLine}";
}
if (result.Properties.TryGetValue("email", out var email) && !string.IsNullOrEmpty(email))
{
AuthToken += $"Email: {email}{Environment.NewLine}";
}
AuthToken += result?.AccessToken ?? result?.IdToken;
IsAuthenticated = true;
}
catch (Exception ex)
{
AuthToken = string.Empty;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
