Friday, September 30, 2011

Creating IP Restriction using asp.net


//IPAddress.csv
"IP Address"
"127.0.0.1"





private void GetIPAddressONFile()
    {
        string SystemIpAddress = "";
        string SystemIpName = "";
     
        SystemIpAddress = Request.UserHostAddress;

        SystemIpName = Request.UserHostName;
        SystemIpAddress = Context.Request.ServerVariables["REMOTE_HOST"];
        string path = Server.MapPath("../IPAddress//IPAddress.csv");
        System.IO.FileInfo file = new FileInfo(path);
        DataSet dsCSV = new DataSet();
        dsCSV = ConnectFile(file);

        //DataColumn tableColumn = dsCSV.Tables[0].Columns["IP Address"];
        DataRowCollection tableRows = dsCSV.Tables[0].Rows;
        int count = 0;
        foreach (DataRow row in tableRows)
        {
            if (row["IP Address"].ToString() == SystemIpAddress)
            {
                count += 1;
            }
        }
        if (count < 1)
            Response.Redirect("~/loginsubmit.aspx?Msg=NA");
    }


 private DataSet ConnectFile(FileInfo filetable)
    {
        DataSet ds = new DataSet();
        string str;
        try
        {
            ConnCSV = ConnCSV + filetable.DirectoryName.ToString();
            string sqlSelect;
            OleDbConnection objOleDBConn;
            OleDbDataAdapter objOleDBDa;
            objOleDBConn = new OleDbConnection(ConnCSV);
            objOleDBConn.Open();
            sqlSelect = "select * from [" + filetable.Name.ToString() + "]";
            objOleDBDa = new OleDbDataAdapter(sqlSelect, objOleDBConn);
            objOleDBDa.Fill(ds);
            objOleDBConn.Close();
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return ds;
    }

No comments:

Post a Comment