Click here to Skip to main content
15,893,381 members
Articles / Web Development / IIS

Browser Detection using ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.63/5 (56 votes)
6 May 2009CPOL5 min read 559.7K   6.3K   186  
Using and updating your machine.config file to better detect browser type in ASP.NET
using System;
using NUnit.Framework;

namespace Ocean.Nunit.HttpCapabilities
{
	/// <summary>
	/// Summary description for Capabilities_OS.
	/// </summary>
	[TestFixture]
	public class Capabilities_OSPlatform
	{
		Ocean.Web.HttpCapabilities.Settings Browser;
		Ocean.Web.HttpCapabilities.Result R;
		public Capabilities_OSPlatform()
		{
			Browser = new Ocean.Web.HttpCapabilities.Settings(Ocean.Web.HttpCapabilities.EmbededFile.BrowserCap);
		}

		[Test]
		public void	Test_Amiga()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; AmigaOS; Chimera)");                        
			Assert.AreEqual("Amiga",R.Platform, "Browser Platform Compare");                 
			Assert.AreEqual("AmigaOS",R.OS, "Browser OS Compare");                 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("IBrowse/2.3 (AmigaOS 3.9)");              
			Assert.AreEqual("Amiga",R.Platform, "Browser Platform Compare");                 
			Assert.AreEqual("AmigaOS 3.9",R.OS, "Browser OS Compare"); 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; IBrowse 3.0; AmigaOS4.0)");                    
			Assert.AreEqual("Amiga",R.Platform, "Browser Platform Compare");                 
			Assert.AreEqual("AmigaOS 4.0",R.OS, "Browser OS Compare"); 
 
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("AmigaVoyager/2.95 (compatible; MC680x0; AmigaOS)");
			Assert.AreEqual("Amiga",R.Platform, "Browser Version Compare");
			Assert.AreEqual("AmigaOS",R.OS, "Browser Version Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("AmigaVoyager/3.2 (AmigaOS/MC680x0)");
			Assert.AreEqual("Amiga",R.Platform, "Browser Version Compare");
			Assert.AreEqual("AmigaOS",R.OS, "Browser Version Compare");

		}
		
		[Test]
		public void	Test_Microsoft_NT4()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101");              
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows NT 4.0",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0; .NET CLR 1.0.3705)");
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows NT 4.0",R.OS, "Browser OS Compare");  

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------            
			R = Browser.Process("Mozilla/4.06 [en] (WinNT; U)");                     
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows NT",R.OS, "Browser OS Compare");    


		}
		[Test]
		public void	Test_Microsoft_2000()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Konqueror2)");       
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 2000",R.OS, "Browser OS Compare");  

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------                  
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 5.11  [en]");    
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 2000",R.OS, "Browser OS Compare"); 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/5.0 (Windows 2000; U) Opera 6.04  [en]");        
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 2000",R.OS, "Browser OS Compare"); 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; (R1 1.3); .NET CLR 1.1.4322)");      
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 2000",R.OS, "Browser OS Compare");  
   
		}
		[Test]
		public void	Test_Microsoft_XP()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/3.0 (Windows NT 5.1; U) Opera 7.23  [en]"); 
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows XP",R.OS, "Browser OS Compare");    

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.0  [en]");       
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows XP",R.OS, "Browser OS Compare");
		}
		[Test]
		public void	Test_Microsoft_2003()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2) Opera 7.51  [en]");  
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows Server 2003",R.OS, "Browser OS Compare");                      

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------			
			R =Browser.Process("Opera/7.23 (Windows NT 5.2; U)  [en]");             
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows Server 2003",R.OS, "Browser OS Compare");   

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.41115)");
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("Microsoft Windows Server 2003",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//This one I am unsure of if it is windows 2003 or a private alpha build
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.0; .NET CLR 2.0.31113)");
			Assert.AreEqual("WinNT",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("Microsoft Windows Server 2003",R.OS, "Browser OS Compare");
		}
		[Test]
		public void Test_Microsoft_311()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/2.0 (compatible; MSIE 3.02; Windows 3.1)"); 
			Assert.AreEqual("Win16",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 3.1",R.OS, "Browser OS Compare");   
		}
		[Test]
		public void Test_Microsoft_95()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/4.06 (Win95; I)");                          
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 95",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) Opera 6.01  [en]");      
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 95",R.OS, "Browser OS Compare");      

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/2.0 (compatible; MSIE 3.01; AK; Windows 95)");                  
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 95",R.OS, "Browser OS Compare");    
		}
		[Test]
		public void Test_Microsoft_98()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/3.0 (Windows 98; U) Opera 6.05  [en]");     
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 98",R.OS, "Browser OS Compare");    
  
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------  
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 4.01; AOL 4.0; Windows 98)");             
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 98",R.OS, "Browser OS Compare");    

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------  
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Win98 A; ATHMWWW1.1; MSOCD;)");      
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 98",R.OS, "Browser OS Compare");    

			R = Browser.Process("Mozilla/4.06 [en] (Win98; I)");                     
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows 98",R.OS, "Browser OS Compare");    
		}
		[Test]
		public void Test_Microsoft_ME()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Windows ME) Opera 5.11 [en]");       
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows ME",R.OS, "Browser OS Compare");    
            
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------            
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 5.5; AOL 5.0; Windows 98; Win 9x 4.90)"); 
			Assert.AreEqual("Win9X",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft Windows ME",R.OS, "Browser OS Compare");    
  
		}
		[Test]
		public void Test_Microsoft_WebTV()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Mozilla/3.0 WebTV/1.2 (compatible; MSIE 2.0)");     
			Assert.AreEqual("WebTV",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Microsoft WebTV",R.OS, "Browser OS Compare");
		}
		[Test]
		public void Test_Macintosh_OSX()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		

			R = Browser.Process("iCab/2.9.5 (Macintosh; U; PPC; Mac OS X)");     
			Assert.AreEqual("MacPPC",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Macintosh OS X",R.OS, "Browser OS Compare");         
		}
		[Test]
		public void Test_Macintosh_68K()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Mozilla/3.01Gold (Macintosh; I; 68K)");             
			Assert.AreEqual("Mac68K",R.Platform, "Browser Platform Compare"); 
			Assert.AreEqual("Macintosh 68K",R.OS, "Browser OS Compare");    

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("iCab/2.8.2 (Macintosh; U; 68K)");     
			Assert.AreEqual("Mac68K",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Macintosh 68K",R.OS, "Browser OS Compare");         
		}
		[Test]
		public void Test_Macintosh_Darwin()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Links (0.98; Darwin 7.3.0 Power Macintosh)");       
			Assert.AreEqual("MacPPC",R.Platform, "Browser Platform Compare"); 
			Assert.AreEqual("Macintosh Darwin",R.OS, "Browser OS Compare");
			Assert.AreEqual(false,R.IsBot, "Browser Bot Compare");       
		}
		[Test]
		public void Test_Macintosh_PPC()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R =Browser.Process("Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC) Opera 5.0  [en]");       
			Assert.AreEqual("MacPPC",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("Macintosh PPC",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------	
			R = Browser.Process("Mozilla/4.0 (compatible; MSIE 4.01; AOL 4.0; Mac_PPC)");                
			Assert.AreEqual("MacPPC",R.Platform, "Browser Platform Compare"); 
			Assert.AreEqual("Macintosh PPC",R.OS, "Browser OS Compare"); 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------	
			R = Browser.Process("Mozilla/5.0 (Macintosh; U; PPC Mac OS X; da-dk) AppleWebKit/85 (KHTML, like Gecko) Safari/85");
			Assert.AreEqual("MacPPC",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("Macintosh OS X",R.OS, "Browser OS Compare");         
		}
		[Test]
		public void Test_linux()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Mozilla/5.0 (compatible; Konqueror/2.2-11; Linux)");
			Assert.AreEqual("Unknown",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("Linux",R.OS, "Browser OS Compare"); 

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Mozilla/5.0 (compatible; Konqueror/3.0-rc1; i686 Linux; 20020104)");    
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");             
			Assert.AreEqual("Linux",R.OS, "Browser OS Compare");  

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("Links (2.1pre9; Linux 2.4.20-gentoo-r5 i686; 141x46)");                 
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");             
			Assert.AreEqual("Linux",R.OS, "Browser OS Compare");  

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------		
			R = Browser.Process("ELinks/0.9.CVS (textmode; Linux 2.6.8.1-ck5 i686; 200x93-3)");       
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");             
			Assert.AreEqual("Linux",R.OS, "Browser OS Compare"); 
			
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040806");     
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");             
			Assert.AreEqual("Linux",R.OS, "Browser OS Compare");  
			
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Opera/7.23 (X11; FreeBSD i386; U)  [en]");          
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");           
			Assert.AreEqual("FreeBSD",R.OS, "Browser OS Compare");
		}
		[Test]
		public void Test_freebsd()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------						
			R = Browser.Process("Links (2.0; FreeBSD 5.0-RELEASE i386; 80x25)");     
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");           
			Assert.AreEqual("FreeBSD",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Links (2.1pre14; FreeBSD 4.10-RELEASE i386; x)");     
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");           
			Assert.AreEqual("FreeBSD",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/5.0 (compatible; Konqueror/2.2.2; FreeBSD)");                   
			Assert.AreEqual("Unknown",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("FreeBSD",R.OS, "Browser OS Compare");

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Opera/7.23 (X11; FreeBSD i386; U)  [en]");          
			Assert.AreEqual("Intel",R.Platform, "Browser Platform Compare");           
			Assert.AreEqual("FreeBSD",R.OS, "Browser OS Compare");
		}

		[Test]
		public void Test_OpenBSD()
		{
			
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Mozilla/5.0 (compatible; Konqueror/3.1; OpenBSD)"); 
			Assert.AreEqual("Unknown",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("OpenBSD",R.OS, "Browser OS Compare");
		}
		[Test]
		public void Test_NetBSD()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R = Browser.Process("Links (2.1pre15; NetBSD 1.6.1 alpha; 110x35)");       
			Assert.AreEqual("Alpha",R.Platform, "Browser Platform Compare"); 
			Assert.AreEqual("NetBSD",R.OS, "Browser OS Compare"); 
		}
		[Test]
		public void Test_SunOS()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:0.9.4.1) Gecko/20020518 Netscape6/6.2.3");   
			Assert.AreEqual("SunOS",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("SunOS",R.OS, "Browser OS Compare");  

			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.0) Gecko/20020605");    
			Assert.AreEqual("SunOS",R.Platform, "Browser Platform Compare");  
			Assert.AreEqual("SunOS",R.OS, "Browser OS Compare");  
		}
		[Test]
		public void Test_IRIX()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.75 [en] (X11; I; IRIX64 6.5 IP30)");
			Assert.AreEqual("SGI IRIX",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("SGI IRIX",R.OS, "Browser OS Compare"); 
		}
		[Test]
		public void Test_OSF1()
		{
			//-------------------------------------------------------------------
			//
			//-------------------------------------------------------------------
			R =Browser.Process("Mozilla/4.76 [en] (X11; U; OSF1 V5.1 alpha)");
			Assert.AreEqual("Alpha",R.Platform, "Browser Platform Compare");
			Assert.AreEqual("OSF1",R.OS, "Browser OS Compare"); 
		}
		[Test]
		public void Test_OpenVMS()
		{
			R =Browser.Process("Mozilla/5.0 (X11; U; OpenVMS AlphaServer_300_4/266; en-US; rv:1.5) Gecko/20040116");     
			Assert.AreEqual("Alpha",R.Platform, "Browser Platform Compare");             
			Assert.AreEqual("OpenVMS",R.OS, "Browser OS Compare");  
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions