CoreWebView2EnvironmentOptions.SetAllowedPortRange Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets the allowed port range restriction for the specified component scope and transport protocol.
public void SetAllowedPortRange(Microsoft.Web.WebView2.Core.CoreWebView2AllowedPortRangeScope scope, Microsoft.Web.WebView2.Core.CoreWebView2TransportProtocolKind protocol, int minPort, int maxPort);
member this.SetAllowedPortRange : Microsoft.Web.WebView2.Core.CoreWebView2AllowedPortRangeScope * Microsoft.Web.WebView2.Core.CoreWebView2TransportProtocolKind * int * int -> unit
Public Sub SetAllowedPortRange (scope As CoreWebView2AllowedPortRangeScope, protocol As CoreWebView2TransportProtocolKind, minPort As Integer, maxPort As Integer)
Parameters
Scope on which restrictions will apply.
- protocol
- CoreWebView2TransportProtocolKind
Transport protocol on which restrictions will apply.
- minPort
- Int32
The minimum allowed port number (inclusive).
- maxPort
- Int32
The maximum allowed port number (inclusive).
Exceptions
Thrown when minPort or maxPort is outside the valid range.
Examples
CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();
try
{
// Set allowed port range for WebRTC UDP traffic (example: ports 10000-20000)
options.SetAllowedPortRange(
CoreWebView2AllowedPortRangeScope.WebRtc,
CoreWebView2TransportProtocolKind.Udp,
10000,
20000);
}
catch (Exception ex)
{
// Handle any errors setting the port range
System.Diagnostics.Debug.WriteLine($"Failed to set WebRTC UDP port range: {ex.Message}");
}
string browserExecutableFolder = null;
if (webView2.CreationProperties?.BrowserExecutableFolder != null)
{
browserExecutableFolder = webView2.CreationProperties.BrowserExecutableFolder;
}
CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(browserExecutableFolder, null, options);
// Configure WebRTC UDP port range if experimental API is available
await webView2.EnsureCoreWebView2Async(environment);
Remarks
This API enables WebView2 to operate within enterprise network or firewall restrictions by limiting network communication to a defined port range. It provides fine-grained control by allowing port restrictions to be applied per network component scope, such as WebRTC.
Currently, only WebRTC UDP Port Range restriction is supported.
minPort and maxPort must be within the range 1025-65535 (inclusive), or must both be the sentinel value 0. minPort must be less than or equal to maxPort. If minPort equals maxPort, the range represents a single port.
Calls with invalid ranges throw an ArgumentException.
A component-specific scope (e.g. WebRtc) always takes precedence over Default for that component in SetAllowedPortRange. Default defines the port range restrictions for all components without specific overrides. Passing (0, 0) for a component scope unsets its specific range restriction and inherits range restriction from Default. If Default is set and a specific scope is unset, that component inherits Default. Passing (1025,65535) for a component scope makes port range unrestricted for that component.
| Network Scope State | Behavior | |
|---|---|---|
| Only Default is set | Default applies port range restrictions to all network components | |
| Default and WebRtc are both set | WebRtc port range restrictions applies to WebRTC; Default applies to others | |
| WebRtc only is set | WebRtc applies port range restrictions only to WebRTC; others unrestricted | |
| Default set and WebRtc reset to (0,0) | Default applies port range restrictions to all and WebRTC inherits Default | |
| Default set and WebRtc set to (1025,65535) | Default applies port range restrictions to all except WebRTC which is unrestricted |