Hello,
Microsoft.Identity.Client applies to MASL authentication and is beyond its scope for Google account logins.
The Microsoft Authentication Library (MSAL) enables developers to acquire security tokens from the Microsoft identity platform to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, other Microsoft APIs, third-party web APIs, or your own web API. MSAL supports many different application architectures and platforms including .NET, JavaScript, Java, Python, Android, and iOS.
For scenarios where you use Oauth to log in to your Google account, you could use the WebAuthenticator provided by Maui to implement this functionality. Please refer to the following documentation and examples.
var authUrl = $"{Your_Google.auth_uri}?response_type=code" +
$"&redirect_uri=com.maui.login://" +
$"&client_id={Your_client_id}" +
$"&scope=https://www.googleapis.com/auth/userinfo.email" +
$"&include_granted_scopes=true" +
$"&state=state_parameter_passthrough_value";
var callbackUrl = "your_callbackUrl";
try
{
var response = await WebAuthenticator.AuthenticateAsync(new WebAuthenticatorOptions()
{
Url = new Uri(authUrl),
CallbackUrl = new Uri(callbackUrl)
});
var codeToken = response.Properties["code"];
var parameters = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("grant_type","authorization_code"),
new KeyValuePair<string,string>("client_id",Your_client_id),
new KeyValuePair<string,string>("redirect_uri",callbackUrl),
new KeyValuePair<string,string>("code",codeToken),
});
HttpClient client = new HttpClient();
var accessTokenResponse = await client.PostAsync(your_token_url, parameters);
// This is a custom class used to deserialize JSON data.
LoginResponse loginResponse;
if (accessTokenResponse.IsSuccessStatusCode)
{
var data = await accessTokenResponse.Content.ReadAsStringAsync();
loginResponse = JsonConvert.DeserializeObject<LoginResponse>(data);
}
}
catch (TaskCanceledException e)
{
// Use stopped auth
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.