Posts

Showing posts from May, 2013

Implementing impersonation in c#

Hi, Today am going to explain about the implementation of impersonation. This code we can use in many contexts like web, windows, services etc. The main things to be a pr requisite are user context should have a proper permissions and necessary references were made.( Please see the ‘usings’ here in the snippet) This impersonation is needed mainly in few cases like to do some server side functionality means printing, network printing accessing, checking available printers, IO operations, etc all comes under particular user context. This thing needs to be done in many cases as the IIS application runs under very less privileges. So the IIS_User need to be impersonated before doing few operations. Here am attaching the complete class for making it more simpler to benefit you people. You just need to copy this class in to your project and use a snippet as like this using (Impersonation impersonation = new Impersonation(impersonationUsername, impersonationDomainName, impersonationP

Generate random string using c#

Hi, Today am blogging a simple concept related to generating a random sring. In realtime it will be used mainly for generating captcha, random password, Temporary objects etc. Here we can send the string length expected. Also You can modify he complexity. Its a simple code and easily understandble. Here is the code snippet. /// /// Creates the random string based on the length provided /// /// Length of the password. /// public static string generateRandomString(int PasswordLength) { string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"; Random randNum = new Random(); char[] chars = new char[PasswordLength]; int allowedCharCount = _allowedChars.Length; for (int i = 0; i < PasswordLength; i++) { chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())]; } return new string(chars); } hope it helps you. Regards, Pavan N

Finding file size in bytes on serverside using c#

Hi, This requirement will come mainly while doing I/O operations (in my opinion it is obviously one of tedious taks carried by server). Here am showing you the snippet of code to find out the number of bytes of a file. here am using this code to validate the size of the file on server side. Which is a common requirement while doing file uploads. Here is the snippet. private static long GetFileSizeOnDisk(string filelocation) { FileInfo info = new FileInfo(file); uint dummy, sectorsPerCluster, bytesPerSector; int result = GetDiskFreeSpaceW(info.Directory.Root.FullName, out sectorsPerCluster, out bytesPerSector, out dummy, out dummy); if (result == 0) throw new Win32Exception(); uint clusterSize = sectorsPerCluster * bytesPerSector; uint hosize; uint losize = GetCompressedFileSizeW(file, out hosize); long size; size = (long)hosize << 32 | losize; return ((size + clusterSize - 1) / clusterSize) * clusterSize; } [DllImport("kernel32.dll")] static e

Number to String conversion

Hi, Generally in finance or banking or this kind of requirement is needed. Its more or less a common thing to convert a number to a string. I used this to convert currency to words format. Here is the snippet. /// /// Numbers to text. /// /// The value. /// public string NumberToText(int value) { string[] num0to10 = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }; string[] num11to20 = new string[] { "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty" }; string[] num20to100 = new string[] { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety&q

Download file using WCF REST services

Hi, Generally we used to download a file using a handlers and sending parameters as a querystring (lets say download.ashx). Here few cases we might need to impersonate the user also. It purely depends on environment. Here is a sample code snippet just for reference. if (extension.Trim() == ".pdf") { context.Response.Clear(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "attachment;filename=" + queryFile + ".pdf"); context.Response.BinaryWrite(System.IO.File.ReadAllBytes(file)); } But instead of that we can download a file as a stream using a WCF REST service too. Here is the code snippet for declaration in interface .. /// <summary> /// To fetch documents /// </summary> /// <param name="filePath">Represent text of filepath</param> /// <param name="fileName">Represents text of filename&l