Beispiel #1
0
        public void GetPupilsTest()
        {
            using var adService = AdService.Login(_user, _pass);
            var pupils = adService.GetPupils("5AHIF");

            Assert.True(pupils.Length > 0);
            Assert.True(pupils.All(p => p.Classes.Contains("5AHIF")));
        }
Beispiel #2
0
        public async Task <(bool success, string?message)> TryLogin(string username, string password)
        {
            if (_httpContextAccessor.HttpContext is null)
            {
                return(false, "Das verwendete Protokoll erlaubt kein Login.");
            }
            var context = _httpContextAccessor.HttpContext;

            try
            {
                using var service = IsDevelopmentMode ? AdService.Login(_searchuser, _searchpass, username) : AdService.Login(username, password);
                var currentUser = service.CurrentUser;
                if (currentUser is null)
                {
                    return(false, "Fehler beim Laden der Benutzerinformationen.");
                }
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, currentUser.Cn),
                    new Claim("Cn", currentUser.Cn),
                    new Claim(ClaimTypes.Role, currentUser.Role.ToString()),
                    new Claim("AdUser", currentUser.ToJson()),
                };
                var claimsIdentity = new ClaimsIdentity(
                    claims,
                    Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                    //AllowRefresh = true,
                    ExpiresUtc = DateTimeOffset.UtcNow.AddHours(3),
                    //IsPersistent = true
                };

                await context.SignInAsync(
                    Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    authProperties);

                return(true, null);
            }
            catch (ApplicationException e)
            {
                return(false, e.Message);
            }
        }
Beispiel #3
0
 public void GetRoleTest()
 {
     using var adService = AdService.Login(_user, _pass);
     Assert.True(adService.CurrentUser.Role == AdUserRole.Teacher);
 }
Beispiel #4
0
 public void LoginSuccessTest()
 {
     using var adService = AdService.Login(_user, _pass);
     Assert.Contains(_user, adService.CurrentUser.Cn);
 }