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

Alachisoft.NCache.Common.Util.SecurityUtil Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.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, String port) {
        boolean isAdministrator = false;
        if (userName == null && password == null) {
            isAdministrator = false;
        }

        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");
            //it can be  something that you use for windows login
            //it can also be
            env.put(Context.SECURITY_PRINCIPAL, userName);
            env.put(Context.SECURITY_CREDENTIALS, password);
            //in following property we specify ldap protocol and connection url.
            //generally the port is 389
            env.put(Context.PROVIDER_URL, domain + ":" + port);
            LdapContext ctx = new InitialLdapContext(env, null);
            isAdministrator = true;
        } catch (NamingException nex) {
            isAdministrator = false;
        }
        return isAdministrator;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy