public async Task <IActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { try { if (ldapService.Login(model.Username, model.Password)) { var userClaims = new List <Claim> { new Claim("username", model.Username), new Claim(ClaimsIdentity.DefaultNameClaimType, model.Username) }; var principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims, ldapService.GetType().Name, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType)); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); return(Redirect(returnUrl ?? Request.PathBase + "/")); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } } return(View(model)); }