Tuesday, May 15, 2012

LDAP Authentication in ASP.Net c#

in config file ..
  <authentication mode="Forms">
              <forms loginUrl="~/Account/LogOn" timeout="10"/>
        </authentication>                  



in code behind

 public  ValidateUser(string user, string pass)
        {
            bool validation;
            try
            {
                LdapConnection ldc = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
                NetworkCredential ncon = new NetworkCredential(user, pass, "DOMAINNAME");
                ldc.Credential = ncon;
                ldc.AuthType = AuthType.Negotiate;
                ldc.Bind(ncon); 
                validation = true;
            }
            catch (LdapException)
            {
                validation = false;
            }
            return validation;
        }

No comments: