Friday, April 22, 2011

Domain Password Authentication From ASP.Net

public bool IsAuthenticated(string username, string pwd)
    {
        string domain = "CorpName"; // Enter Your Corp Name
        string _path = "LDAP://" + domain;
        string domainAndUsername = domain + @"\" + username;
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
        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;
            }
        }
        catch (Exception ex)
        {
            Session["error"] = ex.Message;
            return false;
        }
        return true;
    }

No comments:

Post a Comment