Sunday, April 03, 2011

Check is IP in range

You can find countless examples how to check is IP in range. I would like to offer my way for solution with LINQ:

using System.Collections.Generic;
using System.Net;
using System.Linq;


 public class IPRange
    {
        public byte[] StartAddressBytes { get; private set; }
        public byte[] EndAddressBytes { get; private set; }

        public IPRange(IPAddress startAddress, IPAddress endAddress)
        {
            StartAddressBytes = startAddress.GetAddressBytes();
            EndAddressBytes = endAddress.GetAddressBytes();
        }
        public IPRange(string strartIp, string endIp)
        {
            IPAddress startAddress, endAddress;
            if (IPAddress.TryParse(strartIp, out startAddress) 

                && IPAddress.TryParse(endIp, out endAddress))
            {
                StartAddressBytes = startAddress.GetAddressBytes();
                EndAddressBytes = endAddress.GetAddressBytes();
            }
        }
    }

//--------------------------
public class IPRangeUtil
    {
        public static bool IsInRange(List<
IPRange> ipRange , string clientIp)
        {
            IPAddress clientAddress;
            if (!IPAddress.TryParse(clientIp, out clientAddress))
            {
                return false;
            }
            return IsInRange(ipRange, clientAddress);
        }
        public static bool IsInRange(
List<IPRange> ipRange, IPAddress clientAddress)
        {
            int clientIpSum = clientAddress.GetAddressBytes().Sum(item => item);
            return ipRange.Where(range =>
                clientIpSum > range.StartAddressBytes.Sum(item => item)
                && clientIpSum < range.EndAddressBytes.Sum(item => item)).Any() ;
        }
    }



A using is very easy:
 List ranges = new List
                    {
                        //You can replace "xxx.xxx.xxx.xxx" to IPAddress typed variable 
                          new IPRange("192.168.10.68", "192.168.10.255"),
                          new IPRange("192.168.0.68h", "192.168.10.68")
                    };


bool isInRange = IPRangeUtil.IsInRange(ranges, "192.168.10.70");





Enjoy!





kick it on DotNetKicks.com

Wednesday, March 23, 2011

What is Authentication and Authorization?

Simple video which explain who is who:

Thursday, January 20, 2011

LINQ and #Temporary tables in Stored Procedure do not have return values

I have Stored Procedure which include #tmp table. I tried to drag it to DBML but every time i got message "bla-bla-bla ... do not have return values...".After countless attempts was found solution! (Thanks to Marc Gravell )
Replace the #tmp in variable, a meaning:

CREATE TABLE #tmp (Item in)
Replace it to:
DECLARE @tmp TABLE (Item int)

A using in query is:
SELECT yt.*, t.Item
FROM YUOR_TABLE yt, @tmp t


Enjoy!

Sunday, December 26, 2010

How to encrypt and decrypt password in SQL Server

Here you can find nice and full solution to encrypt and decrypt password for use in SQL Server.

Using:
declare @value VARCHAR(8000)
--Encrypt
set @value =  dbo.fnEncDecRc4('Magic Word','password')
--Decrypt
select dbo.fnEncDecRc4('Magic Word',@value)
Enjoy!

Saturday, December 25, 2010

Log Viewer Pro

Many years I’m use the best file manager Total Commander (Windows commander in the past). In the home site was found link to the site http://www.totalcmd.net which include additional plugins and utilities. But I would  present only one very usability utility – Log Viewer Pro.  Current application dedicated to viewing logs and available to using in every text file . Every level of Log may be define in different color, a program is free (for home users) and not required install.

Screenshot:

Main features of viewer:
Fast scrolling, eats low memory.
Supports any file size (4 Gb and larger).
Multitabbed interface.
Auto-reload file, Follow tail mode.
Allows to highlight some lines (e.g. "errors", "warnings").
Allows to view encodings: ANSI, OEM, Unicode LE, Unicode BE etc.
File search (both forward and backward).

Download here.

Size: 650 KB
Author: Alexey Torgashin

Sunday, December 05, 2010

Snippet editor

In my work I'm often use snippets, but by old habit I created every time new snippet manual. Today found nice solutions to my  suffering -  Snippet Editor by BillMcC. Snippet editor ready to work with all version of Visual Studio, support C# and VB snippet, easy interface and big help to programmer.
Link to download.

Tuesday, June 29, 2010

Generate random string

Current code is generate string in defined length and include chars, numbers and symbols.

public static string GetRandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
int[] symbol = new[] { 33, 36, 38, 64, 94 };
int[] digits = new[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
int symbolsAmount = size / 3;
int digitAmount = size / 3;
for(int i = 0; i < size; i++)
{

int switcher = random.Next(3);
char ch;
if(switcher == 0 && symbolsAmount > 0)
{
int id = random.Next(symbol.Length);
ch = Convert.ToChar(symbol[id]);
symbolsAmount--;
}
else if(switcher == 1 && digitAmount > 0)
{
int id = random.Next(digits.Length);
ch = Convert.ToChar(digits[id]);
digitAmount--;
}
else
{
bool isLower = random.Next(2) == 0 ? false : true;
ch = Convert.ToChar(
Convert.ToInt32(
Math.Floor(26 * random.NextDouble() + ((isLower) ? 65 : 97))
)
);

}
builder.Append(ch);
}

return builder.ToString();
}