AuthService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Blazored.LocalStorage;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Net.Http.Json;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using wispro.sp.share.webViewObject;
  11. using wispro.sp.web.Auth;
  12. using System.Text.Json;
  13. using wispro.sp.web.Models;
  14. using AntDesign;
  15. namespace wispro.sp.web.Services
  16. {
  17. public class AuthService : IAuthService
  18. {
  19. private readonly ILocalStorageService localStorageService;
  20. private readonly IHttpService httpClient;
  21. private readonly AuthenticationStateProvider authenticationStateProvider;
  22. private IUserService UserService;
  23. public AuthService(ILocalStorageService localStorageService, IHttpService httpClient, AuthenticationStateProvider authenticationStateProvider, IUserService UserService)
  24. {
  25. this.localStorageService = localStorageService;
  26. this.httpClient = httpClient;
  27. this.authenticationStateProvider = authenticationStateProvider;
  28. this.UserService = UserService;
  29. }
  30. public async Task<bool> LoginAsync(loginDto userInfo)
  31. {
  32. bool result = false;
  33. var httpResponse = await httpClient.Post<userToken>($"account/Login", userInfo);
  34. Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(httpResponse));
  35. if (httpResponse.StatusCode != System.Net.HttpStatusCode.NotFound)
  36. {
  37. userToken userToken = httpResponse;
  38. await localStorageService.SetItemAsync<userToken>("authToken", userToken);
  39. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserAuthentication(userToken.Token);
  40. result = true;
  41. }
  42. //System.Threading.Thread.Sleep(1000);
  43. return result;
  44. }
  45. public async Task<bool> ChangePassword(changePasswordDto dto)
  46. {
  47. var httpResponse = await httpClient.Post<bool>($"account/ChangePassword", dto);
  48. return httpResponse;
  49. }
  50. public async Task LogoutAsync()
  51. {
  52. await localStorageService.RemoveItemAsync("authToken");
  53. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogOut();
  54. //httpClient.DefaultRequestHeaders.Authorization = null;
  55. }
  56. public async Task<List<string>> GetRoles(string resourceId)
  57. {
  58. var httpResponse = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resourceId}");
  59. return httpResponse;
  60. }
  61. public async Task<bool> CanVisitResource(string resId)
  62. {
  63. bool canVisist = false;
  64. var Roles = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resId}");
  65. CurrentUser _user =await UserService.GetUser();
  66. if (Roles.Count == 0)
  67. {
  68. canVisist = true;
  69. }
  70. else
  71. {
  72. foreach (var uRole in _user.Roles)
  73. {
  74. if (Roles.Contains(uRole))
  75. {
  76. canVisist = true;
  77. break;
  78. }
  79. }
  80. }
  81. return canVisist;
  82. }
  83. }
  84. }