Beispiel #1
0
        private async Task <NetworkPackage> GetRequest(string URI)
        {
            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(URI);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string responseAsJson = await response.Content.ReadAsStringAsync();

                TeacherDataPackage result = JsonSerializer.Deserialize <TeacherDataPackage>(responseAsJson);
                Console.WriteLine(result.ToString());
                return(result);
            }
            return(null);
        }
Beispiel #2
0
        public async Task ValidateLogin(string username, string password)
        {
            Console.WriteLine("Validating log in");
            if (string.IsNullOrEmpty(username))
            {
                throw new Exception("Enter ID");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new Exception("Enter password");
            }

            ClaimsIdentity identity = new ClaimsIdentity();

            try
            {
                string response = await userService.ValidateLoginAsync(username, password);

                NetworkPackage resultUser = JsonSerializer.Deserialize <NetworkPackage>(response);
                User           user       = new User();
                if (resultUser.type.Equals("StudentData"))
                {
                    StudentDataPackage studentDataPackage = JsonSerializer.Deserialize <StudentDataPackage>(response);
                    user.UserName      = studentDataPackage.data.Id;
                    user.Password      = studentDataPackage.data.Password;
                    user.SecurityLevel = 1;
                    CachedStudent      = studentDataPackage.data;
                    RestoreWindowBooleans();
                    studentWindow = true;
                }
                else if (resultUser.type.Equals("TeacherData"))
                {
                    TeacherDataPackage teacherDataPackage = JsonSerializer.Deserialize <TeacherDataPackage>(response);
                    user.UserName      = teacherDataPackage.data.Id;
                    user.Password      = teacherDataPackage.data.Password;
                    user.SecurityLevel = 2;
                    CachedTeacher      = teacherDataPackage.data;
                    RestoreWindowBooleans();
                    teacherWindow = true;
                }
                else if (resultUser.type.Equals("SecretaryData"))
                {
                    SecretaryDataPackage secretaryDataPackage = JsonSerializer.Deserialize <SecretaryDataPackage>(response);
                    user.UserName      = secretaryDataPackage.data.Id;
                    user.Password      = secretaryDataPackage.data.Password;
                    user.SecurityLevel = 3;
                    CachedSecretary    = secretaryDataPackage.data;
                    RestoreWindowBooleans();
                    secretaryWindow = true;
                }

                identity = SetupClaimsForUser(user);
                string serialisedData = JsonSerializer.Serialize(user);
                await jsRuntime.InvokeVoidAsync("sessionStorage.setItem", "currentUser", serialisedData);

                cachedUser = new User();
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                throw e;
            }

            NotifyAuthenticationStateChanged(
                Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity))));
        }