2012-08-14 52 views
0

结构是这样,验证用户在LDAP目录

ou=system,ou=valeteck,cn=mayank 

我必须检查用户输入的密码是正确的,并配以mayank的用户的密码即。

但系统和cn='mayank'有不同的密码。如果我创建的目录输入对象的密码为cn,我没有使用ldap进行身份验证,但是如果使用系统目录及其密码,我会进行身份验证,但是如何检查用户的密码。

+0

可能重复的[如何在.NET中验证LDAP](http://stackoverflow.com/questions/769268/how-to-authenticate-ldap-in-net) – Shai 2012-08-14 12:53:24

+0

[我的回应在这里有帮助吗? ](http://stackoverflow.com/questions/290548/c-sharp-validate-a-username-and-password-against-active-directory/499716#499716) - 这是专门为Active Directory – 2012-08-14 14:57:17

回答

0
private bool LoginS(string userName, string password) 
     { 
      bool authentic = false; 
      try 
      { 
       DirectoryEntry entry = new DirectoryEntry(LDAP-Path, userName, password, AuthenticationTypes.Secure); 
       authentic = true; 


       Console.WriteLine("Authentication successful"); 

      } 
      catch (DirectoryServicesCOMException e) 
      { 
       _logger.Error("Authentification error", e); 
       //User doesnt exist or input is false 

      } 
      return authentic; 
     } 
0

甚至有更简单的方法由Windows API使用advapi32.dll提供给您。

例子:

[DllImport("advapi32.dll", EntryPoint = "LogonUserW", SetLastError = true, CharSet = CharSet.Auto)] 
    public static extern bool LogOnUser(string lpszUserName, string lpszDomain, string lpszPassword, 
     int dwLogOnType, int dwLogOnProvider, ref IntPtr phToken); 

如果用户确实是在域和正确输入它的密码这种方法简单地返回true或false。 然后,您只需制作自己的登录方法即可检查针对advapi32.dll的身份验证。

public ActionResult SignIn(SignInModel model) 
    { 
     string domainName = CheckSignIn.GetDomainName(model.User.UserName); 
     string userName = CheckSignIn.GetUserName(model.User.UserName); 
     IntPtr token = IntPtr.Zero; 
     bool result = CheckSignIn.LogOnUser(userName, domainName, model.User.UniqueUserCode, 2, 0, ref token); 
     if (result) 
     { 
      if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]) && Request.QueryString["ReturnUrl"] != "/") 
      { 
       FormsAuthentication.RedirectFromLoginPage(model.User.UserName, false); 
      } 
      else 
      { 
       FormsAuthentication.SetAuthCookie(model.User.UserName, false); 
       return RedirectToAction("MyVoyages", "Voyage"); 
      } 
     } 
     return SignIn(true); 
    } 

简单,但功能强大。