简体   繁体   中英

How to find given key values (web.config) in Active Directory using C#

I'm new to Web API. I want to connect my PHP website with Web API in C# which will help to log in into website using Windows authentication. I'm specifiying a key="name" and values="DavidR,JohnH" in the web.config file. Only names provided in key values will be able to log into the system.

public bool Post(string user, string pass, string domain)
{
    DirectoryEntry objDirEntry = new DirectoryEntry("LDAP://" + domain, user, pass);

    try
    {
            DirectorySearcher search = new DirectorySearcher(objDirEntry);
            SearchResult result = search.FindOne();
            string[] name = ConfigurationManager.AppSettings["name"].Split(',');

            foreach (var author in name)
            {
                if (result == null)
                {
                    return false;
                }
                else if(result == name)
                {
                    return true;
                }
            }

            return false;
    }
    catch (Exception)
    {
        return false;
    }
}
<appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    <add key="name" value="DavidR,JohnH"/>
</appSettings>

Try this code:

        public bool Post(string user, string pass, string domain)
    {
        DirectoryEntry objDirEntry = new DirectoryEntry("LDAP://" + domain, user, pass);
        try
        {
            DirectorySearcher search = new DirectorySearcher(objDirEntry);
            SearchResult result = search.FindOne();
            if (result == null)
                return false;
            string[] name = ConfigurationManager.AppSettings["name"].Split(',');
            foreach (var author in name)
            {
                if(author.ToLower() == user.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }

This code should works

You can use short code with Linq like this:

        public bool Post(string user, string pass, string domain)
    {
        DirectoryEntry objDirEntry = new DirectoryEntry("LDAP://" + domain, user, pass);
        try
        {
            DirectorySearcher search = new DirectorySearcher(objDirEntry);
            SearchResult result = search.FindOne();
            if (result == null)
                return false;
            string[] name = ConfigurationManager.AppSettings["name"].Split(',');

            if (name.Any(x => x.ToLower() == user.ToLower()))
                return true;
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }

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