简体   繁体   English

在asp.net中的C#中进行LDAP身份验证

[英]LDAP authentication in C# in asp.net

In my web app, I have a login.aspx page. 在我的网络应用程序中,我有一个login.aspx页面。 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 我的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. 在ASP.Net MVC中读过LDAP身份验证,但在成员资格提供程序中,他们用连接,密码和用户名写了。

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. 连接保护,用户名和密码适用于有权代表系统查询AD的帐户。 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. 根据网络的安全性,可能必须进行设置,否则您将无法查询AD以对用户进行身份验证。

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. 如果您需要做的只是验证用户是否存在且密码是否有效,您可以使用以下内容:参数是域名,USER ID和USER密码。 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. 可能的原因是这不适用于所有情况,但到目前为止它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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