Saturday, November 30, 2013

LDAP authentication using JAVA

package eclipsepackage;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.
NamingEnumeration;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.*;

import java.util.*;

public class test {

     final static String ldapServerName = "LDAP://127.0.0.1:389/DC=martinkabraham,DC=com";
     final static String dc1="martinkabraham";
     final static String rootdn = "OU=Martin";
     final static String rootpass = "password";
     final static String rootContext = "OU=Martin";
     final static String username="martin.abraham";
         
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Properties env = new Properties();
       
        env.put( Context.INITIAL_CONTEXT_FACTORY,
                 "com.sun.jndi.ldap.LdapCtxFactory" );
        env.put(Context.REFERRAL, "follow");
        env.put( Context.PROVIDER_URL, ldapServerName);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put( Context.SECURITY_PRINCIPAL, new String(dc1 + "\\"+username) );
        env.put( Context.SECURITY_CREDENTIALS, rootpass );
       
        DirContext ctx = null;
        NamingEnumeration results = null;
        try {
            ctx = new InitialDirContext(env);
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            results = ctx.search("", "(sAMAccountName="+username+")", controls);
            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
             
                if(attributes.get("cn")!=null)
                {
                    // login here .... user exist
                  System.out.println("SUCCESS");
                }
                else
                {
                     System.out.println("F");
               
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception e) {
                }
            }
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception e) {
                }
            }
        }

       
    }

}

Sunday, November 24, 2013

Android Browser File Down Load

Android browser will not download file in button Post events. In post events the file will be some .htm garbage file. to over come this do as below.

In download button click


 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("download-file.aspx");
    }

and on  download-file.aspx file do as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mobile_download_file : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = "usermanual.pdf";
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + "" + filename + "");
        Response.Write(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
        Response.TransmitFile(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
        Response.End();
    }
}