Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> GUID in .NET
GUID in .NET
GUID in .NET


by Dev Vrat Sahu on 9/3/2012 7:35:54 PM

Views: 2437       Comments: 0

GUID in .NET

When Windows developers need a unique value, they often utilize a Globally Unique Identifier (GUID). Microsoft uses the term GUID for a unique number that identifies an entity.

A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required. There's a very low probability that this type of identifier will be duplicated.

This is the format of a typical GUID:

5e4a32f1-3e40-4dd2-b272-ec4763342a8d

Sample Program to Create a GUID in .NET

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //It will always print Unique GUID using NewGuid() of GUID Class.

            Console.WriteLine("GUID : " + System.Guid.NewGuid());

        }

    }

}

 

Now, we’re moving to some advance concept in using GUID.

Here is the sample of a program that will show use the use of Image GUID to validate image format.

While developing any Web Application there may be a requirement to Upload User’s Image to the server,  so we need to validate that image file before uploading to the server, because there may be a chance that user may upload malicious script.

For validating Image File, generally we check the extension of the uploaded file before uploading the file, if found appropriate extension of the file, we proceed it for uploading to the server. But this validation is not quite enough to prevent uploading any malicious script to the server, because user may easily change the extension of script file to upload it to the server.  Then?? How can we tackle this problem?

Using Image GUID, we can overcome to this problem. Following program will show you how to use Image GUID in ASP.NET.

1.      Create a new WebSite- File> New> Website. Select ASP.NET Web Site from the Templates. Choose Location to store the file and Give some name to it. In my case it is Default.aspx

2.      Now, Drag Drop a FileUpload, Label and a Button Control to the Web Page as following.

GUID in .NET

3.      Write following Codes on Click Event Handler of Upload Button.

protected void btnUpload_Click(object sender, EventArgs e)

    {

        string ImageFormat=string.Empty;

        try

        {

            // Checking whether FileUpload has any File Selected or Not.

            if (FileUpload1.HasFile)

            {

                //Retrieving the Content of File uploaded by FileUpload

                System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);

 

                // This will match the GUID of Uploaded File to GUID of Actual Image Format.

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid)

                    ImageFormat = "bmp";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Gif.Guid)

                    ImageFormat = "gif";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Icon.Guid)

                    ImageFormat = "icon";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid)

                    ImageFormat = "jpeg";

                if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Png.Guid)

                    ImageFormat = "png";

                else

                {

                    // It will raise an exception if Uploaded File Extension

                    //not matched with given Image Format Extensions GUID.

                    throw new System.Exception();                    

                }

                lblError.Text = "File Uploaded Successfully.";

            }

        }

        catch (Exception ex)

        {

            lblError.Text = "Attempt to Upload Invalid Image File.";

        }

    }

 

Just try to upload an Image file with file extension bmp, gif, icon, jpeg, png, you will be able to upload these Files.

 

Now, change the extension of any doc,docx or any other file’s extension to bmp, gif, icon, jpeg, png and try upload it.

 

You will get an Error that “Attempt to Upload Invalid Image File”, because here we have put the code to match the GUID of the Uploaded File to GUID of Actual file Format.

 

GUID in .NET

 

 

 

GUID in .NET

 

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Dev Vrat SahuRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5149
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.