Account.razor.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Components;
  4. using Microsoft.AspNetCore.Components.Authorization;
  5. using Microsoft.AspNetCore.Identity;
  6. using HyperCube.Models;
  7. namespace HyperCube.Pages
  8. {
  9. public partial class Account
  10. {
  11. [Inject]
  12. public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  13. [Inject]
  14. public UserManager<IdentityUser> UserManager { get; set; }
  15. private AccountModel account = new();
  16. string myBalance = "";
  17. protected override async Task OnInitializedAsync()
  18. {
  19. account = await GetCurrentAcc();
  20. if (account.GetSelectedBlockChain() != null)
  21. myBalance = await account.GetBalance();
  22. }
  23. private async Task<AccountModel> GetCurrentAcc()
  24. {
  25. AccountModel account = new();
  26. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  27. var user = authState.User;
  28. if (user.Identity.IsAuthenticated)
  29. {
  30. var currentUser = await UserManager.GetUserAsync(user);
  31. var acc = AccountModel.Find(currentUser.Id);
  32. if (acc != null)
  33. account = acc;
  34. //account.AccRole = Role.User;
  35. return account;
  36. }
  37. return null;
  38. }
  39. }
  40. }