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:
- Public Class WebForm1
- Inherits System.Web.UI.Page
-
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
- Try
- AddVenture21UrlToTrustedSitesForIE("http://sdw2078:30/")
- AddVenture21UrlToTrustedSitesForIE("http://10.10.223.27:49181/")
- ApiHostConfigurator.ConfigurApiHost()
- Catch ex As Exception
-
- End Try
-
- End Sub
-
-
-
-
-
-
-
- Private Sub AddVenture21UrlToTrustedSitesForIE(url As String)
- Dim input As String = String.Empty
- Dim ur As New Uri(url)
- Dim browser As String = TrustedSiteRegistry.GetDefaultBrowser()
- Try
- If (browser = "Internet Explorer") Then
- input = ur.Host
- If ur.HostNameType = UriHostNameType.IPv4 Then
- TrustedSiteRegistry.AddIpAddress(input)
- ElseIf ur.HostNameType = UriHostNameType.Dns Then
- TrustedSiteRegistry.SaveToTrustedSite(input)
- End If
- End If
- Catch ex As Exception
-
- End Try
- End Sub
-
- End Class
Step 2
Now create the TrustedSiteRegistry class and write the method for saving it in the Trusted Zone like this:
- Imports Microsoft.Win32
- Imports System.Runtime.CompilerServices
-
-
-
-
-
- Public Class TrustedSiteRegistry
-
- #Region "Constants for Trusted sites"
- Const DomainsKeyLocation As String = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains"
- Const RANGES_KEY As String = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges"
- Const TRUSTED_SITE_CODE As Integer = &H2
- Const ALL_PROTOCOL As String = "http"
- Const RANGE_ADDRESS As String = ":Range"
-
- #End Region
-
-
-
-
-
-
- Public Shared Function GetDefaultBrowser() As String
- Dim returnValue As String = String.Empty
-
- Using baseKey As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Clients\StartmenuInternet")
- Dim baseName As String = baseKey.GetValue("").ToString
- Const software As String = "SOFTWARE\"
- Const path As String = "Clients\StartMenuInternet\"
-
- Dim subKey As String = String.Format("{0}{1}{2}{3}", software, IIf(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") = "AMD64", "Wow6432Node\", ""), path, baseName)
-
- Using browserKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(subKey)
- returnValue = browserKey.GetValue("").ToString
- End Using
- End Using
- Return returnValue
- End Function
-
-
-
-
-
-
- Public Shared Sub SaveToTrustedSite(domain As String)
- Dim subdomains As Dictionary(Of String, String) = New Dictionary(Of String, String)() From { _
- {String.Empty, "http"} _
- }
- Using currentUserKey As RegistryKey = Registry.CurrentUser
- currentUserKey.GetOrCreateSubKey(DomainsKeyLocation, domain)
- For Each subdomain As Object In subdomains
- CreateSubdomainKeyAndValue(currentUserKey, DomainsKeyLocation, domain, CType(subdomain, Global.System.Collections.Generic.KeyValuePair(Of String, String)), TRUSTED_SITE_CODE)
- Next
- End Using
- End Sub
-
-
-
-
-
-
-
-
-
-
-
- Public Shared Sub AddIpAddress(ipAddress As String)
- Dim rangeKey As RegistryKey = GetRangeKey(ipAddress)
- rangeKey.SetValue(ALL_PROTOCOL, TRUSTED_SITE_CODE, RegistryValueKind.DWord)
- rangeKey.SetValue(RANGE_ADDRESS, ipAddress, RegistryValueKind.[String])
- End Sub
-
-
-
-
-
-
-
-
-
- Private Shared Function GetRangeKey(ipAddress As String) As RegistryKey
- Using currentUserKey As RegistryKey = Registry.CurrentUser
- For i As Integer = 1 To Integer.MaxValue - 1
- Dim rangeKey As RegistryKey = currentUserKey.GetOrCreateSubKey(RANGES_KEY, "Range" + i.ToString())
-
- Dim addressValue As Object = rangeKey.GetValue(RANGE_ADDRESS)
- If addressValue Is Nothing Then
- Return rangeKey
- Else
- If Convert.ToString(addressValue) = ipAddress Then
- Return rangeKey
- End If
- End If
- Next
- Throw New Exception("No range slot can be used.")
- End Using
- End Function
-
-
-
-
-
-
-
-
- Private Shared Sub CreateSubdomainKeyAndValue(currentUserKey As RegistryKey, domainsKeyLocation As String, domain As String, subdomain As KeyValuePair(Of String, String), zone As Integer)
- Using subdomainRegistryKey As RegistryKey = currentUserKey.GetOrCreateSubKey(String.Format("{0}\{1}", domainsKeyLocation, domain), subdomain.Key)
- Dim objSubDomainValue As Object = subdomainRegistryKey.GetValue(subdomain.Value)
- If objSubDomainValue Is Nothing OrElse Convert.ToInt32(objSubDomainValue) <> zone Then
- subdomainRegistryKey.SetValue(subdomain.Value, zone, RegistryValueKind.DWord)
- End If
- End Using
- End Sub
-
- End Class
Step 3
Create the Extension method in VB.Net using a module like this:
- Imports Microsoft.Win32
- Imports System.Runtime.CompilerServices
-
-
-
-
-
- Public Module RegistryKeyExtensionMethods
-
-
-
-
-
-
-
-
-
- <Extension()> _
- Public Function GetOrCreateSubKey(registryKey As RegistryKey, parentString As String, subString As String) As RegistryKey
- Dim subKey As RegistryKey = registryKey.OpenSubKey(Convert.ToString(parentString & Convert.ToString("\")) & subString, True)
- If subKey Is Nothing Then
- subKey = registryKey.CreateSubKey(parentString, subString)
- End If
- Return subKey
- End Function
-
-
-
-
-
-
-
-
-
- <Extension()> _
- Public Function CreateSubKey(registryKey As RegistryKey, parentKeyLocation As String, key As String) As RegistryKey
- Dim parentKey As RegistryKey = registryKey.OpenSubKey(parentKeyLocation, True)
- If parentKey Is Nothing Then
- Throw New NullReferenceException(String.Format("Missing parent key: {0}", parentKeyLocation))
- End If
- Dim createdKey As RegistryKey = parentKey.CreateSubKey(key)
- If createdKey Is Nothing Then
- Throw New Exception(String.Format("Key not created: {0}", key))
- End If
- Return createdKey
- End Function
- 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:
Note: If you need to add a complete URL name with .com then you need to give com in the dictionary like this code.
- Dim subdomains As Dictionary(Of String, String) = New Dictionary(Of String, String)() From { _
- {com, "http"} _
- }