All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.security.util.SecurityUtil Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package com.alachisoft.ncache.security.util;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.Hashtable;

public class SecurityUtil {
    public static boolean AuthorizeUserNamePassword(String Domain, String Username, String Password) {
        boolean authenticate;
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");
            env.put(Context.SECURITY_PRINCIPAL, Username);
            env.put(Context.SECURITY_CREDENTIALS, Password);
            env.put(Context.PROVIDER_URL, "LDAP://" + Domain);
            LdapContext ctx = new InitialLdapContext(env, null);
            authenticate = true;
        } catch (NamingException nex) {
            authenticate = false;
        }
        return authenticate;


//            boolean Success = false;
//		System.DirectoryServices.DirectoryEntry Entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + Domain, Username, Password);
//		System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher(Entry);
//		Searcher.SearchScope = System.DirectoryServices.SearchScope.OneLevel;
//		try
//		{
//			System.DirectoryServices.SearchResult Results = Searcher.FindOne();
//			Success = (Results != null);
//		}
//		catch (java.lang.Exception e)
//		{
//			Success = false;
//		}
//		return Success;
    }

    //public static bool AuthorizeUserNamePassword(string domainController, string userName, string password)
    //{
    //    bool valid = false;

    //    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domainController))
    //    {
    //        valid = context.ValidateCredentials(userName, password);
    //    }
    //    return valid;
    //}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy