简体   繁体   English

在ASP.NET 3.5应用程序中使用Active Directory / LDAP登录用户

[英]Using Active Directory/LDAP to Login user in ASP.NET 3.5 app

I have to find a way how to use company Active Direcotries to Login users and I am not sure if there is a certain way to connect without admin credentials (as anonymous) - I need to only search for user if he exist in there. 我必须找到一种方法来使用公司Active Direcotries登录用户,并且我不确定是否存在没有管理员凭据(作为匿名用户)的特定连接方式-我只需要搜索用户(如果该用户存在于其中)。

I tried plenty of solutions but nothing worked yet. 我尝试了很多解决方案,但没有任何效果。 I am new to AD and ASP.NET (but I used to do programming in Java). 我是AD和ASP.NET的新手(但是我以前用Java进行编程)。

The code I've used so far is this from MSDN site: 到目前为止,我使用的代码来自MSDN站点:

        DirectoryEntry entry = new DirectoryEntry(_path);

        try
        {
            //Bind to the native AdsObject to force authentication.
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(sAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();


            if (null == result)
            {
                return false;
            }

            //Update the new path to the user in the directory.
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

        return true;

Where path is variable with link to LDAP (LDAP:///rootDSE) 其中路径是变量,带有指向LDAP(LDAP:/// rootDSE)的链接

Main problem is that i am not alble to trace if i am correctly connected to AD. 主要问题是,如果我正确连接到AD,我将无法跟踪。 Result ends with null, but I am sure I use right credentials for testing. 结果以null结尾,但是我确定我使用正确的凭据进行测试。

Can anyone help or give advice 任何人都可以帮助或提供建议

Thanks a lot 非常感谢

You can go over the 你可以过去

PrincipalContext 主体上下文

Like this: 像这样:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, myDomainTextBox.Text))
{
    // validate the credentials
    bool cIsValid = pc.ValidateCredentials(myUserNameTextBox.Text, myPasswordBox.Password);

    if (cIsValid)
    {
        // Do some stuff
    }
}

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

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