将android连接到openLDAP服务器 [英] connect android to openLDAP server
本文介绍了将android连接到openLDAP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被分配了一个任务,要从android连接到LDAP服务器.从android执行身份验证过程.我正在使用openLDAP.我是一个新手,有人可以帮我解决一些显示android连接LDAP服务器并进行身份验证的代码或演示代码.
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 anyone help me out with some piece of code or demo code showing android connecting with LDAP server and authenticating. The
Port no-389
SSL Port-636
IP Address-LGSPC3
username-->cn=name,cn=users,dc=mydeomain,dc=com
serverlist-ldap://server1
提前谢谢
推荐答案
我用来成功连接到LDAP服务器的代码如下:
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();
}
}
}
}
希望对您有帮助.
这篇关于将android连接到openLDAP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文