Click here to Skip to main content
15,887,915 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I've made a Login with Active Directory in Asp.net and C#, in my Local Pc it works fine, I can Login with Active Directory Users, but after I published the Application in the domain www.mydomain.com it doesn't work.
I don't know if this is happening because to get the connection with Active Directory is necessary to be in the same network of the company, or maybe the company have to give some permissions to LDAP connection to make it public.

This is my code to connect with Active Directory:

C#
user = this.MainLogin.UserName.ToUpper();
pass = this.MainLogin.Password;
domain = "gruposcare";
path = "LDAP://gcsbog-dc00.gruposcare.org.co";

string domainAndUsername = domain + @"\" + user;

DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pass);

try
{
    DirectorySearcher search = new DirectorySearcher(entry);
    SearchResult result = search.FindOne();
    
    if (result == null)
    {
        //Authentication Failed
    }
    else
    {
        //Authentication Right
    }
}
catch (Exception ex)
{
    return false;
}


Thanks for your answers.
Posted
Comments
ZurdoDev 23-Feb-15 14:37pm    
What's the error?
judah9107 23-Feb-15 17:27pm    
Request for the permission of type 'System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.
judah9107 23-Feb-15 14:44pm    
When I test this Login Form in LocalHost it works fine, I mean compiling through VS2010, but after publishing this Login Form in the domain (www.mydomain.com) the authentication doesn't work
judah9107 23-Feb-15 14:50pm    
Maybe it's because when I publish, the network where the application is hosted change, when I test it compiling in my local Pc the Application is in the same network that the company but after publishing I think the Login Form is not in the same network that the company, but I'm not sure if this is the error.
Richard Deeming 23-Feb-15 15:26pm    
The server which hosts the application will need to be able to talk to a server in the AD domain, and will need suitable DNS configuration so that it can resolve the server name. Your network administrators should be able to help you resolve the issue.

1 solution

Hi, you place your logic in a service or publish it on the server. Instead of writing the complete logic on aspx code behind. Also, add below filters in you code:

user = this.MainLogin.UserName.ToUpper();
pass = this.MainLogin.Password;
domain = "gruposcare";
path = "LDAP://gcsbog-dc00.gruposcare.org.co";

string domainAndUsername = domain + @"\" + user;

DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pass);

try
{
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if (result == null)
{
//Authentication Failed
}
else
{
//Authentication Right
}
}
catch (Exception ex)
{
return false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900