AuthService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if (httpResponse.StatusCode != System.Net.HttpStatusCode.NotFound)
  35. {
  36. userToken userToken = httpResponse;
  37. await localStorageService.SetItemAsync<userToken>("authToken", userToken);
  38. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserAuthentication(userToken.Token);
  39. result = true;
  40. }
  41. //System.Threading.Thread.Sleep(1000);
  42. return result;
  43. }
  44. public async Task<bool> ChangePassword(changePasswordDto dto)
  45. {
  46. var httpResponse = await httpClient.Post<bool>($"account/ChangePassword", dto);
  47. return httpResponse;
  48. }
  49. public async Task LogoutAsync()
  50. {
  51. await localStorageService.RemoveItemAsync("authToken");
  52. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogOut();
  53. //httpClient.DefaultRequestHeaders.Authorization = null;
  54. }
  55. public async Task<List<string>> GetRoles(string resourceId)
  56. {
  57. var httpResponse = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resourceId}");
  58. return httpResponse;
  59. }
  60. public async Task<bool> CanVisitResource(string resId)
  61. {
  62. bool canVisist = false;
  63. var Roles = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resId}");
  64. CurrentUser _user =await UserService.GetUser();
  65. if (Roles.Count == 0)
  66. {
  67. canVisist = true;
  68. }
  69. else
  70. {
  71. foreach (var uRole in _user.Roles)
  72. {
  73. if (Roles.Contains(uRole))
  74. {
  75. canVisist = true;
  76. break;
  77. }
  78. }
  79. }
  80. return canVisist;
  81. }
  82. }
  83. }