Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AuthClientService.cs 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Grpc.Net.Client;
  2. using GrpcShared.DTO.Auth;
  3. using GrpcShared.Interfaces;
  4. using NemAnBlazor.Services.Interfaces;
  5. using ProtoBuf.Grpc.Client;
  6. using GrpcShared;
  7. using GrpcShared.DTO.User;
  8. using GrpcShared.DTO;
  9. using System.Security.Claims;
  10. using Microsoft.AspNetCore.Components.Authorization;
  11. using Blazored.SessionStorage;
  12. namespace NemAnBlazor.Services
  13. {
  14. public class AuthClientService : AuthenticationStateProvider, IAuthClientService
  15. {
  16. private IAuthService _serviceClient;
  17. private ISessionStorageService _sessionStorage;
  18. public AuthClientService(GrpcChannel grpcChannel, ISessionStorageService sessionStorage)
  19. {
  20. _serviceClient = grpcChannel.CreateGrpcService<IAuthService>();
  21. _sessionStorage = sessionStorage;
  22. }
  23. public async Task<TokenResponse> GetAccessToken(TokenRequest request)
  24. {
  25. return await _serviceClient.GetAccessToken(request);
  26. }
  27. public async Task<CodeRequest> GetAuthParams()
  28. {
  29. return await _serviceClient.GetAuthParams();
  30. }
  31. public async Task<UserInfoResponse> GetUserInfo(TokenMessage token)
  32. {
  33. return await _serviceClient.GetUserInfo(token);
  34. }
  35. public override async Task<AuthenticationState> GetAuthenticationStateAsync()
  36. {
  37. await Task.Delay(1500);
  38. string token = await _sessionStorage.GetItemAsync<string>("token");
  39. //token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN";
  40. if (token == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  41. var userInfo = await _serviceClient.GetUserInfo(new TokenMessage { Token = token });
  42. List<Claim> claims = new();
  43. claims.Add(new Claim("email", userInfo.email!));
  44. claims.Add(new Claim("id", userInfo.id!));
  45. claims.Add(new Claim("name", userInfo.display_name!));
  46. ClaimsIdentity identity = new(claims, "jwt");
  47. //ClaimsIdentity identity = new();
  48. ClaimsPrincipal user = new(identity);
  49. AuthenticationState state = new(user);
  50. NotifyAuthenticationStateChanged(Task.FromResult(state));
  51. return state;
  52. }
  53. }
  54. }