AuthService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. using Microsoft.AspNetCore.Components;
  16. using Microsoft.JSInterop;
  17. namespace wispro.sp.web.Services
  18. {
  19. public class AuthService : IAuthService
  20. {
  21. private readonly ILocalStorageService localStorageService;
  22. private readonly IHttpService httpClient;
  23. private readonly AuthenticationStateProvider authenticationStateProvider;
  24. private IUserService UserService;
  25. private NavigationManager NavigationManager;
  26. private MessageService _message;
  27. private IJSRuntime JSRuntime;
  28. public AuthService(ILocalStorageService localStorageService, IHttpService httpClient, AuthenticationStateProvider authenticationStateProvider, IUserService UserService, MessageService message, IJSRuntime _JSRuntime, NavigationManager navigation)
  29. {
  30. this.localStorageService = localStorageService;
  31. this.httpClient = httpClient;
  32. this.authenticationStateProvider = authenticationStateProvider;
  33. this.UserService = UserService;
  34. this._message = message;
  35. this.JSRuntime = _JSRuntime;
  36. this.NavigationManager = navigation;
  37. }
  38. public async Task<bool> LoginAsync(loginDto userInfo)
  39. {
  40. bool result = false;
  41. var httpResponse = await httpClient.Post<userToken>($"account/Login", userInfo);
  42. //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(httpResponse));
  43. if (httpResponse.StatusCode != System.Net.HttpStatusCode.NotFound)
  44. {
  45. userToken userToken = httpResponse;
  46. await localStorageService.SetItemAsync<userToken>("authToken", userToken);
  47. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserAuthentication(userToken.Token);
  48. result = true;
  49. }
  50. //System.Threading.Thread.Sleep(1000);
  51. return result;
  52. }
  53. public async Task<bool> ChangePassword(changePasswordDto dto)
  54. {
  55. var httpResponse = await httpClient.Post<bool>($"account/ChangePassword", dto);
  56. return httpResponse;
  57. }
  58. public async Task LogoutAsync()
  59. {
  60. await localStorageService.RemoveItemAsync("authToken");
  61. ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogOut();
  62. //httpClient.DefaultRequestHeaders.Authorization = null;
  63. }
  64. public async Task<List<string>> GetRoles(string resourceId)
  65. {
  66. var httpResponse = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resourceId}");
  67. return httpResponse;
  68. }
  69. public async Task CanVisitResource()
  70. {
  71. string strUrl = NavigationManager.Uri;
  72. //Console.WriteLine($"Uri:{strUrl}");
  73. //Console.WriteLine($"Uri:{NavigationManager.BaseUri}");
  74. string strResourceId = NavigationManager.Uri.Replace(NavigationManager.BaseUri, "/");
  75. bool canVisist = false;
  76. var Roles = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={strResourceId}");
  77. CurrentUser _user =await UserService.GetUser();
  78. if (Roles.Count == 0)
  79. {
  80. canVisist = true;
  81. }
  82. else
  83. {
  84. foreach (var uRole in _user.Roles)
  85. {
  86. if (Roles.Contains(uRole))
  87. {
  88. canVisist = true;
  89. break;
  90. }
  91. }
  92. }
  93. if (!canVisist)
  94. {
  95. var config = new MessageConfig()
  96. {
  97. Content = "您没有权限使用该功能",
  98. Type = MessageType.Error
  99. };
  100. var ret = _message.Open(config);
  101. await JSRuntime.InvokeVoidAsync("history.back");
  102. }
  103. }
  104. }
  105. }