connect android to openLDAP server

前端 未结 1 856
长发绾君心
长发绾君心 2021-01-16 08:47

I have been assigned a task to, connect to a LDAP server from android.Perform the authetication process from android.I am using openLDAP. I am completely new to this.Can any

1条回答
  •  余生分开走
    2021-01-16 09:20

    The code I used to connect to LDAP server successfully is as follow:

    import java.sql.Date;
    import java.text.SimpleDateFormat;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    import com.unboundid.ldap.sdk.AddRequest;
    import com.unboundid.ldap.sdk.LDAPConnection;
    import com.unboundid.ldap.sdk.LDAPException;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    //String address="ldap://server1.mydomain.com" ;
    String address="xxx.xxx.xxx.xxx";
    int port=389;
    //String bindDN="CN=name,CN=users,DC=mydomain,DC=com";
    String bindDN="cn=Manager,dc=maxcrc,dc=com";
    String password="secret";
    boolean login_flag=true;
    
    LDAPConnection c ;
    AddRequest addRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button bt_login=(Button)findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @SuppressLint("SimpleDateFormat") @Override
    public void onClick(View arg0) {
    
           try {
               c= new LDAPConnection(address,port,bindDN,password);
               c.setConnectionName("Demo Connection");
               String con_name=c.getConnectionName();
               long time=c.getConnectTime();
               SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
               String dateString = formatter.format(new Date(time));
               Toast.makeText(getBaseContext(),"Connected to LDAP server....connection_name="+con_name+" at time"+dateString, Toast.LENGTH_LONG).show();
    
          } catch (LDAPException e) {
              login_flag=false;
              e.printStackTrace();
              Toast.makeText(getBaseContext(),"No connection was established" , Toast.LENGTH_LONG).show();
        } catch(Exception e) {
               e.printStackTrace();
        } finally{
               if(login_flag){
                     c.close();
                     Toast.makeText(getBaseContext(), "Connection Closed successfully", Toast.LENGTH_LONG).show();
               }
       }
    
    
    }
    
    }
    

    Hope it helps you.

    0 讨论(0)
提交回复
热议问题