Created
March 30, 2025 05:16
-
-
Save hatsunea/fddd35f940d44ba1330ac22963717f0d to your computer and use it in GitHub Desktop.
MauiAuthApp.Models.AuthenticationModel.LoginAsync.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MauiAuthApp.Models | |
{ | |
internal class AuthenticationModel : IDisposable | |
{ | |
public async Task<string> LoginAsync() | |
{ | |
try | |
{ | |
var authService = new AuthenticationService(); | |
var result = await authService.LoginAsync(CancellationToken.None); | |
var token = result?.IdToken; | |
if (token != null) | |
{ | |
var handler = new JwtSecurityTokenHandler(); | |
var data = handler.ReadJwtToken(token);//JwtSecurityTokenHandlerを使ってトークンをデータ化する | |
if (data != null) | |
{ | |
var stringBuilder = new StringBuilder(); | |
stringBuilder.AppendLine($"ClientId: {data.Audiences.FirstOrDefault()}"); | |
stringBuilder.AppendLine($"aud: {data.Claims.FirstOrDefault(x => x.Type.Equals("aud"))?.Value}"); | |
stringBuilder.AppendLine($"Name: {data.Claims.FirstOrDefault(x => x.Type.Equals("name"))?.Value}"); | |
stringBuilder.AppendLine($"Email: {data.Claims.FirstOrDefault(x => x.Type.Equals("preferred_username"))?.Value}"); | |
token = stringBuilder.ToString(); | |
} | |
} | |
return token ?? "Empty"; | |
} | |
catch (MsalClientException ex) | |
{ | |
return ex.Message; | |
} | |
} | |
public void Dispose() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment