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();
    }
}

Saturday, December 11, 2010

OAuth

 OAuth an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.

 Almost all Social networks  provide a method to share there data  with data with others by the method of OAuth. By this Authorization  for example if i am integrating Twitter with my website the user dosen't need to provide the uname/password of him in my site but they can authenticate in twitter itself and he can give allow permission so that he can view his Tweets and post of others and can Tweet from mysite to Twitter...
This is a  good technology as OpenID. very very Secure.

I have implemented it already for Picassa,Twitter,Facebook and Youtube. for example u can check this site in development stage.

http://208.109.209.105:1081/phaseoflife/

Monday, August 2, 2010

Prevent Pages from going to history on Back Button click using Javascript

Hi we can prevent the  pages ability to go back by using java script. Suppose From Default.aspx page we go to Default2.aspx and we dont want to go to default.aspx from default2.aspx. for this we can place make place javascript on  default.aspx.
 

Saturday, July 31, 2010

ViewState and Hacking ViewState Data In ASP.NET Beware!!!!!!!!

Asp.net View State help us to retrieve data after Postbacks. But  remember this data is generally not safe as it is simply serialized  and stored in the rendered HTML.

 Suppose  this is the Server Code

Wednesday, July 28, 2010

URL Mapping in ASP.NET

In an ideal world, we'd design our Web applications perfectly the first time. Pages would be created in the proper folder and stay there. For that matter, end users would never care about the URLs of the pages in our Web applications, so we could put pages anywhere we wanted without worrying about it.

Get Number of mysql connections if Connection are not cached

string  Qry=" FLUSH STATUS ;SHOW GLOBAL STATUS LIKE '%Max_used_connections%' ";
MysqlDataAdapter ad =new MysqlDataAdapter(Qry);
so fill that to dataset and u will get that.

If connection is cached u need to do work on thread cache and work on  that.