AuthService.cs 5.5 KB

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