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.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AuthClientService.cs 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.LocalStorage;
  12. namespace NemAnBlazor.Services
  13. {
  14. public class AuthClientService : AuthenticationStateProvider, IAuthClientService
  15. {
  16. private IAuthService _serviceClient;
  17. private ILocalStorageService _sessionStorage;
  18. public AuthClientService(GrpcChannel grpcChannel, ILocalStorageService 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 override async Task<AuthenticationState> GetAuthenticationStateAsync()
  28. //{
  29. // string token = await _sessionStorage.GetItemAsync<string>("token");
  30. // //token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN";
  31. // if (token == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  32. // var userInfo = await _serviceClient.GetUserInfo(new TokenMessage ( token ));
  33. // List<Claim> claims = new();
  34. // claims.Add(new Claim("email", userInfo.email!));
  35. // claims.Add(new Claim("id", userInfo.id!));
  36. // claims.Add(new Claim("name", userInfo.display_name!));
  37. // ClaimsIdentity identity = new(claims, "jwt");
  38. // //ClaimsIdentity identity = new();
  39. // ClaimsPrincipal user = new(identity);
  40. // AuthenticationState state = new(user);
  41. // NotifyAuthenticationStateChanged(Task.FromResult(state));
  42. // return state;
  43. //}
  44. public async Task<CodeRequest> GetAuthParams()
  45. {
  46. return await _serviceClient.GetAuthParams();
  47. }
  48. public async Task<UserInfoResponse> GetUserInfo(TokenMessage token)
  49. {
  50. return await _serviceClient.GetUserInfo(token);
  51. }
  52. public override async Task<AuthenticationState> GetAuthenticationStateAsync()
  53. {
  54. await Task.Delay(1500);
  55. string token = await _sessionStorage.GetItemAsync<string>("token");
  56. //token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN";
  57. if (token == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  58. var userInfo = await _serviceClient.GetUserInfo(new TokenMessage{Token = token});
  59. List<Claim> claims = new();
  60. claims.Add(new Claim("email", userInfo.Email!));
  61. claims.Add(new Claim("id", userInfo.Id!));
  62. claims.Add(new Claim("name", userInfo.DisplayName!));
  63. ClaimsIdentity identity = new(claims, "jwt");
  64. //ClaimsIdentity identity = new();
  65. ClaimsPrincipal user = new(identity);
  66. AuthenticationState state = new(user);
  67. NotifyAuthenticationStateChanged(Task.FromResult(state));
  68. return state;
  69. }
  70. }
  71. }