MarkOfTheWeb Class
- Supported on windows
Provides methods to interact with the Mark of the Web (MOTW) security feature in Windows. MOTW helps protect users from potentially unsafe content downloaded from the internet by storing the origin zone information in an Alternate Data Stream (ADS) named "Zone.Identifier".
[SupportedOSPlatform("windows")]
public static class MarkOfTheWebExample
Add, read, and remove the Mark of the Web of a file:
var path = Path.GetTempFileName();
// Add the Mark of the Web to a file
MarkOfTheWeb.SetFileZone(path, UrlZone.Internet);
// Get the zone of a file
var zone = MarkOfTheWeb.GetFileZone(path);
Assert.Equal(UrlZone.Internet, zone);
// Remove the Mark of the Web from a file
MarkOfTheWeb.RemoveFileZone(path);
Assert.Null(MarkOfTheWeb.GetFileZoneContent(path));Methods
| Name | Description |
|---|---|
| GetFileZone(string) | Gets the security zone of a file using the Windows Security Manager COM API. |
| GetFileZoneContent(string) | Gets the raw content of the Zone.Identifier alternate data stream from a file. |
| GetFileZoneIdentifier(string) | Gets the zone information stored in the Zone.Identifier alternate data stream of a file. |
| IsUntrusted(string) | Determines whether a file is from an untrusted source based on its security zone. Files outside of the Local Machine, Trusted, and Intranet zones are considered untrusted. |
| RemoveFileZone(string) | Removes the Mark of the Web zone information from a file by deleting the Zone.Identifier alternate data stream. |
| SetFileZone(string, UrlZone, string?, string?) | Sets the Mark of the Web zone information for a file by writing to the Zone.Identifier alternate data stream. |
GetFileZone(string)
MarkOfTheWeb.csGets the security zone of a file using the Windows Security Manager COM API.
Parameters
filePathstring- The path to the file to query.
Returns
UrlZone The UrlZone of the file, or UrlZone.Invalid if the zone cannot be determined. Windows can also report a custom zone, numbered 1000 and above, that has no named UrlZone member.
GetFileZoneContent(string)
MarkOfTheWeb.csGets the raw content of the Zone.Identifier alternate data stream from a file.
Parameters
filePathstring- The path to the file to read.
Returns
string? The content of the Zone.Identifier stream, or null if the file does not have zone information, or if the file or its directory does not exist.
Remarks
Windows and web browsers write the stream as ASCII. A byte order mark, if present, takes precedence, so streams written as UTF-16 by earlier versions of this library are still decoded correctly.
GetFileZoneIdentifier(string)
MarkOfTheWeb.csGets the zone information stored in the Zone.Identifier alternate data stream of a file.
public static ZoneIdentifier? GetFileZoneIdentifier(string filePath)Parameters
filePathstring- The path to the file to read.
Returns
ZoneIdentifier? The parsed content of the Zone.Identifier stream, or null if the file does not have zone information, or if the file or its directory does not exist.
Remarks
This reports what the stream records, not the zone Windows assigns to the file. Use MarkOfTheWeb.GetFileZone or MarkOfTheWeb.IsUntrusted to make a security decision.
IsUntrusted(string)
MarkOfTheWeb.csDetermines whether a file is from an untrusted source based on its security zone. Files outside of the Local Machine, Trusted, and Intranet zones are considered untrusted.
Parameters
filePathstring- The path to the file to check.
Returns
bool true if the file is from an untrusted zone (Internet or Restricted), or if Windows cannot evaluate its zone; otherwise, false.
Exceptions
FileNotFoundExceptionfilePathdoes not point to an existing file. A directory is not a file.
RemoveFileZone(string)
MarkOfTheWeb.csRemoves the Mark of the Web zone information from a file by deleting the Zone.Identifier alternate data stream.
Parameters
filePathstring- The path to the file from which to remove the zone information.
Remarks
Does nothing when the file carries no zone information, or when the file or its directory does not exist. The zone information of a read-only file is removed too, and the file stays read-only.
SetFileZone(string, UrlZone, string?, string?)
MarkOfTheWeb.csSets the Mark of the Web zone information for a file by writing to the Zone.Identifier alternate data stream.
public static void SetFileZone(string filePath, UrlZone zone, string? referrerUrl = null, string? hostUrl = null)Parameters
filePathstring- The path to the file to mark.
zoneUrlZone- The security zone to assign to the file.
referrerUrlstring?- Optional URL of the page that linked to the file. An empty string is treated as no URL.
hostUrlstring?- Optional URL of the host from which the file was downloaded. An empty string is treated as no URL.
Exceptions
ArgumentOutOfRangeExceptionzoneis not one ofUrlZone.LocalMachine,UrlZone.Intranet,UrlZone.Trusted,UrlZone.Internet, orUrlZone.Untrusted.ArgumentExceptionreferrerUrlorhostUrlcontains a carriage return, a line feed, or a null character.FileNotFoundExceptionfilePathdoes not point to an existing file. A directory is not a file.
Remarks
The zone information of a read-only file is set too, and the file stays read-only.