简体   繁体   中英

LDAP authentication in C# in asp.net

In my web app, I have a login.aspx page. When I enter the username and password, I need to validate it on a remote server. They only provide me the server name (path), and domain name (don't know the password and username). How can it be done?

My web.config

  <authentication mode="Forms">
    <forms loginUrl="Login.aspx" timeout="10"/>
  </authentication>

I have read LDAP Authentication in ASP.Net MVC but, in the membership provider, they wrote in the connection, password and username.

What should i do?

Like the answer there says:

The connection protection, user name and pwd are for the account that has access to query AD on behalf of the system. Depending on the security of your network this may have to be setup or you won't be able to query AD to authenticate the user.

It depends on the configuration of the server how you should authenticate.

If all you need to do is verify that the user exists and the password is valid, you can use something like this: The arguments are domain name, USER id and USER password. Since we're not querying anything, non-admin privileges are OK.

public static bool LogonValid(string ldapDomain, string userName, string password) {
    DirectoryEntry de = new DirectoryEntry(@"LDAP://" + ldapDomain, userName, password);

  try {
    object o = de.NativeObject;
    return true;
  }
    catch (Exception ex) {
    logger.Error(ex.ToString());
    return false;
  }
}

There are probably reasons that this won't work in every situation, but it's worked for me so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM