You can do this using the registry. You need to first add your URL to the registry of IE then the IE browser will read the data from the registry.

In the registry there will be two locations. If you need to add a domain name like: http://www.c-sharpcorner.com/ then you need to store this value in this location:

"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains"

If you need to store an IP address in the IE registry then you to store the key and value in this location:

"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges"

Now we will create a sample code for saving in the registry.

Step 1

To create the form write the code like this:

  1. Public Class WebForm1
  2. Inherits System.Web.UI.Page
  3. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4. Try
  5. AddVenture21UrlToTrustedSitesForIE("http://sdw2078:30/")
  6. AddVenture21UrlToTrustedSitesForIE("http://10.10.223.27:49181/")
  7. ApiHostConfigurator.ConfigurApiHost()
  8. Catch ex As Exception
  9. 'code for exception logger.
  10. End Try
  11. End Sub
  12. ''' <summary>
  13. ''' This is used to validated the input and pass the data for saving in registry.
  14. ''' </summary>
  15. ''' <param name="url">String</param>
  16. ''' <remarks></remarks>
  17. Private Sub AddVenture21UrlToTrustedSitesForIE(url As String)
  18. Dim input As String = String.Empty
  19. Dim ur As New Uri(url)
  20. Dim browser As String = TrustedSiteRegistry.GetDefaultBrowser()
  21. Try
  22. If (browser = "Internet Explorer") Then
  23. input = ur.Host
  24. If ur.HostNameType = UriHostNameType.IPv4 Then
  25. TrustedSiteRegistry.AddIpAddress(input)
  26. ElseIf ur.HostNameType = UriHostNameType.Dns Then
  27. TrustedSiteRegistry.SaveToTrustedSite(input)
  28. End If
  29. End If
  30. Catch ex As Exception
  31. 'code for exception logger.
  32. End Try
  33. End Sub
  34. End Class

Step 2

Now create the TrustedSiteRegistry class and write the method for saving it in the Trusted Zone like this:

  1. Imports Microsoft.Win32
  2. Imports System.Runtime.CompilerServices
  3. ''' <summary>
  4. ''' This class hold the function and method related with saving Trusted Site in Registry.
  5. ''' </summary>
  6. ''' <remarks></remarks>
  7. Public Class TrustedSiteRegistry
  8. #Region "Constants for Trusted sites"
  9. Const DomainsKeyLocation As String = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains"
  10. Const RANGES_KEY As String = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges"
  11. Const TRUSTED_SITE_CODE As Integer = &H2
  12. Const ALL_PROTOCOL As String = "http"
  13. Const RANGE_ADDRESS As String = ":Range"
  14. #End Region
  15. ''' <summary>
  16. ''' Returns the default Browser name.
  17. ''' </summary>
  18. ''' <returns></returns>
  19. ''' <remarks></remarks>
  20. Public Shared Function GetDefaultBrowser() As String
  21. Dim returnValue As String = String.Empty
  22. Using baseKey As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Clients\StartmenuInternet")
  23. Dim baseName As String = baseKey.GetValue("").ToString
  24. Const software As String = "SOFTWARE\"
  25. Const path As String = "Clients\StartMenuInternet\"
  26. 'key value for the default browser
  27. Dim subKey As String = String.Format("{0}{1}{2}{3}", software, IIf(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") = "AMD64", "Wow6432Node\", ""), path, baseName)
  28. 'Default browser name
  29. Using browserKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(subKey)
  30. returnValue = browserKey.GetValue("").ToString
  31. End Using
  32. End Using
  33. Return returnValue
  34. End Function
  35. ''' <summary>
  36. ''' This method is used save data as Trusted Site in IE.
  37. ''' </summary>
  38. ''' <param name="domain">String</param>
  39. ''' <remarks></remarks>
  40. Public Shared Sub SaveToTrustedSite(domain As String)
  41. Dim subdomains As Dictionary(Of String, String) = New Dictionary(Of String, String)() From { _
  42. {String.Empty, "http"} _
  43. }
  44. Using currentUserKey As RegistryKey = Registry.CurrentUser
  45. currentUserKey.GetOrCreateSubKey(DomainsKeyLocation, domain)
  46. For Each subdomain As Object In subdomains
  47. CreateSubdomainKeyAndValue(currentUserKey, DomainsKeyLocation, domain, CType(subdomain, Global.System.Collections.Generic.KeyValuePair(Of String, String)), TRUSTED_SITE_CODE)
  48. Next
  49. End Using
  50. End Sub
  51. ''' <summary>
  52. ''' This method is used to add the IP address.
  53. ''' </summary>
  54. ''' <param name="ipAddress">String</param>
  55. ''' <remarks></remarks>
  56. Public Shared Sub AddIpAddress(ipAddress As String)
  57. Dim rangeKey As RegistryKey = GetRangeKey(ipAddress)
  58. rangeKey.SetValue(ALL_PROTOCOL, TRUSTED_SITE_CODE, RegistryValueKind.DWord)
  59. rangeKey.SetValue(RANGE_ADDRESS, ipAddress, RegistryValueKind.[String])
  60. End Sub
  61. ''' <summary>
  62. ''' This method is used to Get the Range key.
  63. ''' </summary>
  64. ''' <param name="ipAddress">String</param>
  65. ''' <returns></returns>
  66. ''' <remarks></remarks>
  67. Private Shared Function GetRangeKey(ipAddress As String) As RegistryKey
  68. Using currentUserKey As RegistryKey = Registry.CurrentUser
  69. For i As Integer = 1 To Integer.MaxValue - 1
  70. Dim rangeKey As RegistryKey = currentUserKey.GetOrCreateSubKey(RANGES_KEY, "Range" + i.ToString())
  71. Dim addressValue As Object = rangeKey.GetValue(RANGE_ADDRESS)
  72. If addressValue Is Nothing Then
  73. Return rangeKey
  74. Else
  75. If Convert.ToString(addressValue) = ipAddress Then
  76. Return rangeKey
  77. End If
  78. End If
  79. Next
  80. Throw New Exception("No range slot can be used.")
  81. End Using
  82. End Function
  83. ''' <summary>
  84. ''' This method is used to Create Subdomain Key And Value.
  85. ''' </summary>
  86. ''' <param name="domainsKeyLocation">String</param>
  87. ''' <param name="domain">String</param>
  88. ''' <param name="subdomain">KeyValuePair</param>
  89. ''' <param name="zone">Integer</param>
  90. ''' <remarks></remarks>
  91. Private Shared Sub CreateSubdomainKeyAndValue(currentUserKey As RegistryKey, domainsKeyLocation As String, domain As String, subdomain As KeyValuePair(Of String, String), zone As Integer)
  92. Using subdomainRegistryKey As RegistryKey = currentUserKey.GetOrCreateSubKey(String.Format("{0}\{1}", domainsKeyLocation, domain), subdomain.Key)
  93. Dim objSubDomainValue As Object = subdomainRegistryKey.GetValue(subdomain.Value)
  94. If objSubDomainValue Is Nothing OrElse Convert.ToInt32(objSubDomainValue) <> zone Then
  95. subdomainRegistryKey.SetValue(subdomain.Value, zone, RegistryValueKind.DWord)
  96. End If
  97. End Using
  98. End Sub
  99. End Class

Step 3

Create the Extension method in VB.Net using a module like this:

  1. Imports Microsoft.Win32
  2. Imports System.Runtime.CompilerServices
  3. ''' <summary>
  4. ''' This Module hold the Extension method of Registry.
  5. ''' </summary>
  6. ''' <remarks></remarks>
  7. Public Module RegistryKeyExtensionMethods
  8. ''' <summary>
  9. ''' This extension method is used to read the existing key and Create the Key.
  10. ''' </summary>
  11. ''' <param name="registryKey">RegistryKey</param>
  12. ''' <param name="parentString">String</param>
  13. ''' <param name="subString">String</param>
  14. ''' <returns>RegistryKey</returns>
  15. ''' <remarks></remarks>
  16. <Extension()> _
  17. Public Function GetOrCreateSubKey(registryKey As RegistryKey, parentString As String, subString As String) As RegistryKey
  18. Dim subKey As RegistryKey = registryKey.OpenSubKey(Convert.ToString(parentString & Convert.ToString("\")) & subString, True)
  19. If subKey Is Nothing Then
  20. subKey = registryKey.CreateSubKey(parentString, subString)
  21. End If
  22. Return subKey
  23. End Function
  24. ''' <summary>
  25. ''' This extension method is used to Create Sub key.
  26. ''' </summary>
  27. ''' <param name="registryKey">RegistryKey</param>
  28. ''' <param name="parentKeyLocation">String</param>
  29. ''' <param name="key">String</param>
  30. ''' <returns>RegistryKey</returns>
  31. ''' <remarks></remarks>
  32. <Extension()> _
  33. Public Function CreateSubKey(registryKey As RegistryKey, parentKeyLocation As String, key As String) As RegistryKey
  34. Dim parentKey As RegistryKey = registryKey.OpenSubKey(parentKeyLocation, True)
  35. If parentKey Is Nothing Then
  36. Throw New NullReferenceException(String.Format("Missing parent key: {0}", parentKeyLocation))
  37. End If
  38. Dim createdKey As RegistryKey = parentKey.CreateSubKey(key)
  39. If createdKey Is Nothing Then
  40. Throw New Exception(String.Format("Key not created: {0}", key))
  41. End If
  42. Return createdKey
  43. End Function
  44. End Module

Step 4

Now run the application and check in the trusted site list in IE browser. You will see it as in the following:

trusted sites

Note: If you need to add a complete URL name with .com then you need to give com in the dictionary like this code.

  1. Dim subdomains As Dictionary(Of String, String) = New Dictionary(Of String, String)() From { _
  2. {com, "http"} _
  3. }