Asp.net mvc 5 带LDAP身份验证的OWIN
这是我的设想。我有一个MVC5应用程序,它使用Owin作为身份验证机制。默认模板在登录操作中调用SignInManager.PasswordSignInAsync,我希望覆盖该操作以使用LDAP验证用户,而不是查看数据库 我可以通过以下方式进行验证:Asp.net mvc 5 带LDAP身份验证的OWIN,asp.net-mvc-5,ldap,owin,Asp.net Mvc 5,Ldap,Owin,这是我的设想。我有一个MVC5应用程序,它使用Owin作为身份验证机制。默认模板在登录操作中调用SignInManager.PasswordSignInAsync,我希望覆盖该操作以使用LDAP验证用户,而不是查看数据库 我可以通过以下方式进行验证: PrincipalContext dc = new PrincipalContext(ContextType.Domain, "domain.com", "DC=domain,DC=com", "user_name", "password");
PrincipalContext dc = new PrincipalContext(ContextType.Domain, "domain.com", "DC=domain,DC=com", "user_name", "password");
bool authenticated = dc.ValidateCredentials(userName, password);
然后,我可以使用以下方法检索UserPrincipal:
UserPrincipal user = UserPrincipal.FindByIdentity(dc, IdentityType.SamAccountName, userName);
但是,我被困在这里,不知道如何继续登录用户。目标是在我登录用户后,我将有权访问user.Identity,包括用户所处的所有角色。从本质上讲,应用程序的行为应该像使用Windows身份验证一样,但凭据由用户在登录页面上提供
您可能会问,为什么不直接使用Windows身份验证。该应用程序将从网络外部访问,但要求使用广告认证和授权。这就是我的困境
非常感谢您的任何建议
谢谢。经过数小时的研究和反复试验,我终于做到了:
foreach (var group in Request.LogonUserIdentity.Groups) { string role = new SecurityIdentifier(group.Value).Translate(typeof(NTAccount)).Value; string clean = role.Substring(role.IndexOf("\\") + 1, role.Length - (role.IndexOf("\\") + 1)); claims.Add(new Claim(ClaimTypes.Role, clean)); } claims.Add(new Claim(ClaimTypes.NameIdentifier, model.Email)); claims.Add(new Claim(ClaimTypes.Name, model.Email)); ClaimsIdentity ci = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = false, ExpiresUtc = DateTime.UtcNow.AddDays(7), }, ci); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid login credentials."); return View(model); }
Daniel D.要创建PrincipalContext实例,[用户名]和[密码]将来自被授予查询LDAP权限的帐户?正确。此帐户必须能够在LDAP中查找用户。这太棒了!非常感谢。我花了几个小时在MVC中搜索LDAP身份验证的最佳选项。你是个救命恩人!谢谢。很高兴我能帮上忙:) AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;