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 LoginAsync(loginDto userInfo) { bool result = false; var httpResponse = await httpClient.Post($"account/Login", userInfo); Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(httpResponse)); if (httpResponse.StatusCode != System.Net.HttpStatusCode.NotFound) { userToken userToken = httpResponse; await localStorageService.SetItemAsync("authToken", userToken); ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserAuthentication(userToken.Token); result = true; } //System.Threading.Thread.Sleep(1000); return result; } public async Task ChangePassword(changePasswordDto dto) { var httpResponse = await httpClient.Post($"account/ChangePassword", dto); return httpResponse; } public async Task LogoutAsync() { await localStorageService.RemoveItemAsync("authToken"); ((JwtAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogOut(); //httpClient.DefaultRequestHeaders.Authorization = null; } public async Task> GetRoles(string resourceId) { var httpResponse = await httpClient.Get>($"account/GetRoles?ResourceId={resourceId}"); return httpResponse; } public async Task CanVisitResource(string resId) { bool canVisist = false; var Roles = await httpClient.Get>($"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; } } }