123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using Blazored.LocalStorage;
- using Microsoft.AspNetCore.Components.Authorization;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text;
- using System.Threading.Tasks;
- using wispro.sp.share.webViewObject;
- using wispro.sp.web.Auth;
- using System.Text.Json;
- using wispro.sp.web.Models;
- using AntDesign;
- namespace wispro.sp.web.Services
- {
- public class AuthService : IAuthService
- {
- private readonly ILocalStorageService localStorageService;
- private readonly IHttpService httpClient;
- private readonly AuthenticationStateProvider authenticationStateProvider;
- private IUserService UserService;
- public AuthService(ILocalStorageService localStorageService, IHttpService httpClient, AuthenticationStateProvider authenticationStateProvider, IUserService UserService)
- {
- this.localStorageService = localStorageService;
- this.httpClient = httpClient;
- this.authenticationStateProvider = authenticationStateProvider;
- this.UserService = UserService;
- }
-
- public async Task<bool> LoginAsync(loginDto userInfo)
- {
- bool result = false;
- var httpResponse = await httpClient.Post<userToken>($"account/Login", userInfo);
- Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(httpResponse));
- if (httpResponse.StatusCode != System.Net.HttpStatusCode.NotFound)
- {
- userToken userToken = httpResponse;
- await localStorageService.SetItemAsync<userToken>("authToken", userToken);
- ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserAuthentication(userToken.Token);
-
- result = true;
- }
- //System.Threading.Thread.Sleep(1000);
- return result;
- }
- public async Task<bool> ChangePassword(changePasswordDto dto)
- {
- var httpResponse = await httpClient.Post<bool>($"account/ChangePassword", dto);
- return httpResponse;
- }
- public async Task LogoutAsync()
- {
- await localStorageService.RemoveItemAsync("authToken");
- ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogOut();
- //httpClient.DefaultRequestHeaders.Authorization = null;
- }
- public async Task<List<string>> GetRoles(string resourceId)
- {
-
- var httpResponse = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resourceId}");
- return httpResponse;
-
- }
- public async Task<bool> CanVisitResource(string resId)
- {
- bool canVisist = false;
- var Roles = await httpClient.Get<List<string>>($"account/GetRoles?ResourceId={resId}");
- CurrentUser _user =await UserService.GetUser();
- if (Roles.Count == 0)
- {
- canVisist = true;
- }
- else
- {
- foreach (var uRole in _user.Roles)
- {
- if (Roles.Contains(uRole))
- {
- canVisist = true;
- break;
- }
- }
-
- }
- return canVisist;
-
- }
- }
- }
|