CHttpClient::GetSecurityZoneId Method  
 
BOOL GetSecurityZoneId(
  LPCTSTR lpszResource,  
  LPDWORD lpdwZoneId  
);

The GetSecurityZoneId method returns the security zone identifier for the specified resource.

Parameters

lpszResource
A null-terminated string which specifies the resource URL to check. This must be either a valid HTTP URL or a local file URL.
lpdwZoneId
A pointer to an unsigned integer which is updated with the zone ID for the specified resource. It may be one of the following values when the method returns:
Value Description
SECURITY_ZONE_NONE The security zone for this resource cannot be determined.
SECURITY_ZONE_LOCAL The resource is considered to be part of the local computer zone.
SECURITY_ZONE_INTRANET The resource originates from the local intranet zone, such as an internal corporate network.
SECURITY_ZONE_TRUSTED The resource originates from a site in the Trusted Sites zone.
SECURITY_ZONE_INTERNET The resource originates from the Internet and is treated with caution by Windows.
SECURITY_ZONE_RESTRICTED The resource originates from a site in the Restricted Sites zone, which is used for untrusted content.

Return Value

If the method succeeds, the return value is non-zero and lpdwZoneId will be updated with the zone identifier. If the security zone could not be determined, lpdwZoneId will be set to SECURITY_ZONE_NONE. If the method fails, it returns zero and the error code can be retrieved by calling the GetLastError method.

Remarks

This GetSecurityZoneId method is used to determine how a resource should be handled in terms of local security policy before performing a file transfer or download. For example, you may want to determine whether a resource originates from the Internet or local intranet before deciding whether to attach zone metadata, prompt the user, or apply additional security restrictions. It can also be used to implement policy-based rules for accepting or rejecting downloads based on their origin.

The lpszResource parameter must use the http://, https:// or file:// schemes. This method will not accept URLs for other schemes such as ftp:// or sftp://. While this method does not verify that the resource exists, it does require the URL to be syntactically valid. If the host name cannot be resolved to an IPv4 or IPv6 address, the method will fail. If the resource is a local file, it must be a regular file and cannot be the name of a folder or device.

If the resource is specified using a file:// URL that refers to a file on a network share (e.g.: file://server/share/file.txt), the method will attempt to access that file across the network. This may cause the application to block while Windows resolves the hostname and establishes a connection to the shared volume. If the network share is unavailable or slow to respond, the method may block until the connection succeeds or the attempt times out.

If this method returns FALSE, this indicates either the resource type is not supported or there was an internal failure when the Internet Security Manager attempted to map the resource URL to a security zone. In most default configurations, this method will return the zone SECURITY_ZONE_INTERNET for a valid URL unless the host has been explicitly added to the Intranet Zone or Trusted Sites list on the local system.

Security zones are used with Mark of the Web (MOTW) metadata which indicates if a file originated from an external source, such as the Internet or an untrusted network. When a file is downloaded using a web browser or other applications, it may add a Zone.Identifier alternate data stream to the file. This stream contains information such as ZoneId=3, which corresponds to the Internet Zone. Your application can use the GetFileZoneInfo method to retrieve this information, which allows you to determine whether a file was downloaded from an external source and what zone Windows has associated with it. This metadata affects how files are handled by Windows security components such as Microsoft Defender, SmartScreen, Office Protected View, and script execution policies in Windows Explorer. If the file has no metadata, it is generally treated as coming from the Local Machine Zone (zone 0). If your application sets or clears this metadata using the SetFileZoneInfo method, it can influence how other applications interpret the file's trust level.

It is important to note that this method is used to determine what security zone would be appropriate for a given resource, and it only performs syntactic checks and classification of the URL using the IInternetSecurityManager COM interface. The security zone this method returns is based on the current system and user policy, using zone assignments stored in the registry. Typically this information is managed using Windows settings or Internet options in the control panel. This method does not read the zone identifier metadata which has been assigned to a file.

For more detailed information about the zone identifiers, refer to the SetDefaultZoneId method.

Example

LPCTSTR pszDownloadUrl = _T("https://example.com/pub/download/file.txt");
DWORD dwZoneId = SECURITY_ZONE_NONE;

if (httpClient.GetSecurityZoneId(pszDownloadUrl, &dwZoneId))
{
    // dwZoneId now contains SECURITY_ZONE_INTERNET
    _tprintf(_T("The security zone is: %u\n"), dwZoneId);
}
else
{
    DWORD dwError = httpClient.GetLastError();
    // Handle the error
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshttpv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also