Microsoft KB Archive/320375

= HOW TO: Create GUIDs by Using Active Server Pages in IIS =

Article ID: 320375

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Internet Information Services 6.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0
 * Microsoft Internet Information Services 5.1

-



This article was previously published under Q320375



We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx



IN THIS TASK

 * SUMMARY
 * Create a Simple Time-Based GUID
 * Create a Simple Time-Offset GUID
 * Create a More Unique Time-Offset GUID
 * Create a Simple Random GUID
 * Create a Variable-Length Random GUID
 * Create a Windows-Style Random GUID
 * Create a Stronger Windows-Style Random GUID
 * REFERENCES

back to top



SUMMARY
This step-by-step article describes how to create several example pages by using various forms of GUIDs for Active Server Pages (ASP) pages to use. These values can be used to create unique data entries in a database, or anywhere else where a unique data field is required.

NOTE: Because these examples do not use session variables, these examples also work on Web servers that have session state disabled.

back to top

Create a Simple Time-Based GUID
This example creates a simple 14-character time-based GUID by using the current year, month, day, hour, minute, and seconds. This permits you to sort data based on the GUID.  Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.  Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%>   <%

Response.Write &quot;GUID = &quot; & CreateGUID

Function CreateGUID Dim tmpTemp tmpTemp = Right(String(4,48) & Year(Now),4) tmpTemp = tmpTemp & Right(String(4,48) & Month(Now),2) tmpTemp = tmpTemp & Right(String(4,48) & Day(Now),2) tmpTemp = tmpTemp & Right(String(4,48) & Hour(Now),2) tmpTemp = tmpTemp & Right(String(4,48) & Minute(Now),2) tmpTemp = tmpTemp & Right(String(4,48) & Second(Now),2) CreateGUID = tmpTemp End Function %>    Save the page:  On the File menu, click Save.</li> Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> Name the file GuidTest0.asp .</li> Click Save.</li> On the File menu, click Exit.</li></ol> </li> Locate the page with Microsoft Internet Explorer. You see a GUID. As you refresh the page, you see the GUID increment.</li></ol>

back to top

Create a Simple Time-Offset GUID
This example creates a 20-character time-based GUID by using the offset in seconds from 12:00 midnight on January 1, 2000. This permits you to sort data based on the GUID, and can be updated to use any other date as the base. <ol> Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li>  Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%>   <%

Response.Write &quot;GUID = &quot; & CreateGUID

Function CreateGUID Dim tmpTemp1,tmpTemp2 tmpTemp1 = Right(String(15,48) & CStr(CLng(DateDiff(&quot;s&quot;,&quot;1/1/2000&quot;,Date))), 15) tmpTemp2 = Right(String(5,48) & CStr(CLng(DateDiff(&quot;s&quot;,&quot;12:00:00 AM&quot;,Time))), 5) CreateGUID = tmpTemp1 & tmpTemp2 End Function %> </BODY> </HTML> </li> Save the page: <ol style="list-style-type: lower-alpha;"> On the File menu, click Save.</li> Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> Name the file GuidTest1.asp .</li> Click Save.</li> On the File menu, click Exit.</li></ol> </li> Locate the page with Internet Explorer. You see a GUID. As you refresh the page, you see the GUID increment.</li></ol>

NOTE: This example permits uniqueness only when the function is not called two times in the same second. Any two calls to the function at the same second cause a collision. This may not cause a problem depending on how your code is using the function, but you can use the Create a More Unique Time-Offset GUID example to allow unique values that occur in the same second.

back to top

Create a More Unique Time-Offset GUID
This example, like the Create a Simple Time-Offset GUID example, creates a time-based GUID, but it is 25 characters long. This example creates a 20-character value from the offset in seconds from 12:00 midnight on January 1, 2000, and appends an additional five random numbers to the end of that. This permits you to sort data based on the GUID, but permits much greater uniqueness. This can be updated to use any other date as the base. <ol> <li>Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li> <li> Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%> <HTML> <BODY> <%

Response.Write &quot;GUID = &quot; & CreateGUID

Function CreateGUID Randomize Timer Dim tmpTemp1,tmpTemp2,tmpTemp3 tmpTemp1 = Right(String(15,48) & CStr(CLng(DateDiff(&quot;s&quot;,&quot;1/1/2000&quot;,Date))), 15) tmpTemp2 = Right(String(5,48) & CStr(CLng(DateDiff(&quot;s&quot;,&quot;12:00:00 AM&quot;,Time))), 5) tmpTemp3 = Right(String(5,48) & CStr(Int(Rnd(1) * 100000)),5) CreateGUID = tmpTemp1 & tmpTemp2 & tmpTemp3 End Function %> </BODY> </HTML> </li> <li>Save the page: <ol style="list-style-type: lower-alpha;"> <li>On the File menu, click Save.</li> <li>Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> <li>Name the file GuidTest2.asp .</li> <li>Click Save.</li> <li>On the File menu, click Exit.</li></ol> </li> <li>Locate the page with Internet Explorer. You see a GUID. As you refresh the page, you see the first 20 characters of the GUID increment, and the last five characters are random.</li></ol>

NOTE: This example and the Create a Simple Time-Offset GUID example are created for time-based serialization of calls to the CreateGUID function. If sorting data based on time is not a requirement, you can use the Create a Simple Random GUID example to create a random GUID.

back to top

Create a Simple Random GUID
<ol> <li>Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li> <li> Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%> <HTML> <BODY> <%

Response.Write &quot;GUID = &quot; & CreateGUID

Function CreateGUID Randomize Timer Dim tmpCounter,tmpGUID Const strValid = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot; For tmpCounter = 1 To 20 tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1) Next CreateGUID = tmpGUID End Function %> </BODY> </HTML> </li> <li>Save the page: <ol style="list-style-type: lower-alpha;"> <li>On the File menu, click Save.</li> <li>Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> <li>Name the file GuidTest3.asp .</li> <li>Click Save.</li> <li>On the File menu, click Exit.</li></ol> </li> <li>Locate the page with Internet Explorer. You see a 20-character random GUID. As you refresh the page, you see the value change randomly.</li></ol>

NOTE: This example creates a fixed-length random GUID. To create a variable-length GUID, use the Create a Variable-Length Random GUID example.

back to top

Create a Variable-Length Random GUID
<ol> <li>Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li> <li> Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%> <HTML> <BODY> <%

Response.Write &quot;<P>GUID = &quot; & CreateGUID(10) Response.Write &quot;<P>GUID = &quot; & CreateGUID(25) Response.Write &quot;<P>GUID = &quot; & CreateGUID(50)

Function CreateGUID(tmpLength) Randomize Timer Dim tmpCounter,tmpGUID Const strValid = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot; For tmpCounter = 1 To tmpLength tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1) Next CreateGUID = tmpGUID End Function %> </BODY> </HTML> </li> <li>Save the page: <ol style="list-style-type: lower-alpha;"> <li>On the File menu, click Save.</li> <li>Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> <li>Name the file GuidTest4.asp .</li> <li>Click Save.</li> <li>On the File menu, click Exit.</li></ol> </li> <li>Locate the page with Internet Explorer. You see a 10-character, 25-character, and 50-character random GUID. As you refresh the page, you see the values change randomly.</li></ol>

NOTE: This example and the Create a Simple Random GUID example both create strings of concatenated random characters. The Create a Windows-Style Random GUID example expands on this functionality to create a GUID that is formatted like a Microsoft Windows GUID.

back to top

Create a Windows-Style Random GUID
Windows primarily uses GUIDs when it registers Class IDs for components and other objects, but GUIDs can be used for anything. Windows GUIDs are long strings of formatted hexadecimal characters, meaning that they use the numbers 0 through 9 and the letters A through F. (Microsoft Access also has a built-in Replication ID in the same format.) This example shows how to use code that is similar to the Create a Variable-Length Random GUID example code to create a GUID like a Windows GUID. <ol> <li>Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li> <li> Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%> <HTML> <BODY> <%

Response.Write &quot;<P>GUID = &quot; & CreateWindowsGUID

Function CreateWindowsGUID CreateWindowsGUID = CreateGUID(8) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(12) End Function

Function CreateGUID(tmpLength) Randomize Timer Dim tmpCounter,tmpGUID Const strValid = &quot;0123456789ABCDEF&quot; For tmpCounter = 1 To tmpLength tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1) Next CreateGUID = tmpGUID End Function %> </BODY> </HTML> </li> <li>Save the page: <ol style="list-style-type: lower-alpha;"> <li>On the File menu, click Save.</li> <li>Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> <li>Name the file GuidTest5.asp .</li> <li>Click Save.</li> <li>On the File menu, click Exit.</li></ol> </li> <li>Locate the page with Internet Explorer. You see a random GUID that is formatted like a Windows GUID. As you refresh the page, you see the value change randomly.</li></ol>

back to top

Create a Stronger Windows-Style Random GUID
Because Windows GUIDs use hexadecimal numbers for each character, 16 values are possible per character in the GUID. If you expand the values to include the whole alphabet, 36 values are possible per character in the GUID. This example uses the function from the Create a Variable-Length Random GUID example to create a GUID in the same format as a Windows GUID. <ol> <li>Click Start, point to Programs, click Accessories, and then click Notepad to open Notepad.</li> <li> Type or paste the following ASP code in Notepad: <%@LANGUAGE=&quot;VBSCRIPT&quot;%> <HTML> <BODY> <%

Response.Write &quot;<P>GUID = &quot; & CreateWindowsGUID

Function CreateWindowsGUID CreateWindowsGUID = CreateGUID(8) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(4) & &quot;-&quot; & _ CreateGUID(12) End Function

Function CreateGUID(tmpLength) Randomize Timer Dim tmpCounter,tmpGUID Const strValid = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot; For tmpCounter = 1 To tmpLength tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1) Next CreateGUID = tmpGUID End Function %> </BODY> </HTML> </li> <li>Save the page: <ol style="list-style-type: lower-alpha;"> <li>On the File menu, click Save.</li> <li>Locate the root folder for your Web site, which is typically the C:\InetPub\Wwwroot folder for the default Web site.</li> <li>Name the file GuidTest6.asp .</li> <li>Click Save.</li> <li>On the File menu, click Exit.</li></ol> </li> <li>Locate the page with Internet Explorer. You see a random GUID that is formatted like a Windows GUID. As you refresh the page, you see the value change randomly.</li></ol>

back to top

<div class="references_section">