public void Test() { LdapAuthenticationManager ldapAuthenticationManager = new LdapAuthenticationManager(ConfigurationManager.AppSettings["Ldap_ConnectionString"]); var x = ldapAuthenticationManager.ValidateUser("", ""); Console.WriteLine(x.ToString()); }
public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { var ldapAuthenticationManager = new LdapAuthenticationManager(ConfigurationManager.AppSettings["Ldap_ConnectionString"]); var userManager = new UserManager(); var signInManager = new SignInManager(AuthenticationManager); var identityUserService = new IdentityUserService(); try { var user = await userManager.FindByNameAsync(model.UserName); if (user != null) { if (user.Logins.Any(l => l.LoginProvider == "Ldap")) { var result = ldapAuthenticationManager.ValidateUser(model.UserName, model.Password); switch (result) { case SignInStatus.Success: var identity = await identityUserService.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity); return(RedirectToLocal(returnUrl)); default: ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } } else { ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } } else { var result = ldapAuthenticationManager.ValidateUser(model.UserName, model.Password); switch (result) { case SignInStatus.Success: var ldapUser = new User() { Name = model.UserName, SecurityStamp = Guid.NewGuid().ToString() }; await userManager.CreateAsync(ldapUser); await userManager.AddLoginAsync(ldapUser, new UserLoginInfo("Ldap", "")); var identity = await identityUserService.CreateIdentityAsync(ldapUser, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity); return(RedirectToLocal(returnUrl)); default: ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } } } finally { ldapAuthenticationManager.Dispose(); userManager.Dispose(); signInManager.Dispose(); } }