FeaturePolicyBuilder

We found 10 examples in language CSharp for this search. You will see 34 fragments of code.

        public FeaturePolicyBuilder WithAccelerometer(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.Accelerometer.UnionWith(sources);
            return this;
        }

        public FeaturePolicyBuilder WithAmbientLightSensor(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.AmbientLightSensor.UnionWith(sources);
            return this;
        }

        public FeaturePolicyBuilder WithAutoplay(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.Autoplay.UnionWith(sources);
            return this;
        }

        public FeaturePolicyBuilder WithCamera(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.Camera.UnionWith(sources);
            return this;
        }

        public FeaturePolicyBuilder WithEncryptedMedia(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.EncryptedMedia.UnionWith(sources);
            return this;
        }

        public FeaturePolicyBuilder WithFullscreen(params string[] sources)
        {
            if (sources == null || sources.Length < 1)
                throw new ArgumentException("Default sources have not been set.", nameof(sources));

            Policy.Fullscreen.UnionWith(sources);
            return this;
        }

        [Fact]
        public void Build_WhenNoValues_ReturnsNull()
        {
            var builder = new FeaturePolicyBuilder();

            var result = builder.Build();

            result.Should().BeNullOrEmpty();
        }

        [Fact]
        public void Build_AddAccelerometer_WhenAddsMultipleValue_ReturnsAllValues()
        {
            var builder = new FeaturePolicyBuilder();
            builder.AddAccelerometer()
                .Self()
                .For("http://testUrl.com");

            var result = builder.Build();

            result.Should().Be("accelerometer 'self' http://testUrl.com");
        }

        [Fact]
        public void Build_AddAccelerometer_WhenIncludesNone_OnlyWritesNone()
        {
            var builder = new FeaturePolicyBuilder();
            builder.AddAccelerometer()
                .Self()
                .For("http://testUrl.com")
                .None();

            var result = builder.Build();

            result.Should().Be("accelerometer 'none'");
        }

        [Fact]
        public void Build_AddAccelerometer_WhenIncludesAll_OnlyWritesAll()
        {
            var builder = new FeaturePolicyBuilder();
            builder.AddAccelerometer()
                .Self()
                .For("http://testUrl.com")
                .All();

            var result = builder.Build();

            result.Should().Be("accelerometer *");
        }

        [Fact]
        public void Build_AddAccelerometer_WhenIncludesAllAndNone_ThrowsInvalidOperationException()
        {
            var builder = new FeaturePolicyBuilder();
            builder.AddAccelerometer()
                .None()
                .All();

            Assert.Throws<InvalidOperationException>(() => builder.Build());
        }

        [Fact]
        public void Build_CustomFeatureDirective_AddsValues()
        {
            var builder = new FeaturePolicyBuilder();
            builder.AddCustomFeature("push", "'none'");
            builder.AddCustomFeature("vibrate", "*");
            builder.AddCustomFeature("something-else", string.Empty);

            var result = builder.Build();

            result.Should().Be("push 'none'; vibrate *; something-else");
        }

        [Fact]
        public void PaymentFromNowhere_SetsValue()
        {
            var builder = new FeaturePolicyBuilder();

            builder.AllowPayment.FromNowhere();
            var options = builder.BuildFeaturePolicyOptions();
            string headerValue = options.ToString();

            Assert.Equal("payment 'none'", headerValue);
        }

        [Fact]
        public void PaymentFromSelfAndGoogle_SetsValue()
        {
            var builder = new FeaturePolicyBuilder();

            builder.AllowPayment.FromSelf().From("https://www.google.com");
            var options = builder.BuildFeaturePolicyOptions();
            string headerValue = options.ToString();

            Assert.Equal("payment 'self' https://www.google.com", headerValue);
        }

        [Fact]
        public void PaymentFromNowhereAndFullScreenFromSelf_SetsValue()
        {
            var builder = new FeaturePolicyBuilder();

            builder.AllowPayment.FromNowhere();
            builder.AllowFullscreen.FromSelf();
            var options = builder.BuildFeaturePolicyOptions();
            string headerValue = options.ToString();

            Assert.Equal("fullscreen 'self'; payment 'none'", headerValue);
        }

        [Fact]
        public void OtherNewFeatureFromSelf_SetsValue()
        {
            var builder = new FeaturePolicyBuilder();

            builder.AllowOtherFeature("new-feature").FromSelf();
            var options = builder.BuildFeaturePolicyOptions();
            string headerValue = options.ToString();

            Assert.Equal("new-feature 'self'", headerValue);
        }


        /// <summary>
        /// Configure a feature policy.
        /// </summary>
        /// <param name="configure">Configure the Feature-Policy header</param>
        /// <returns>The complete Feature-Policy header</returns>
        public static FeaturePolicyHeader Build(Action<FeaturePolicyBuilder> configure)
        {
            var builder = new FeaturePolicyBuilder();

            configure(builder);

            return new FeaturePolicyHeader(builder.Build());
        }


        /// <summary>
        /// Configure a feature policy.
        /// </summary>
        /// <param name="configure">Configure the Feature-Policy header</param>
        /// <returns>The complete Feature-Policy header</returns>
        public static FeaturePolicyHeader Build(Action<FeaturePolicyBuilder> configure)
        {
            var builder = new FeaturePolicyBuilder();

            configure(builder);

            return new FeaturePolicyHeader(builder.Build());
        }


    /// <summary>
    /// This replaces the deprecated VR Feature
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static CustomFeaturePolicyDirectiveBuilder AddXR(this FeaturePolicyBuilder builder) => builder.AddCustomFeature("xr-spatial-tracking");

        [Fact]
        public void FeatureBuilderTest()
        {
            var builder = Builders.FeaturePolicyBuilder.Build(new FeaturePolicy
            {
                Autoplay = new HashSet<string> { Source.Self, "http://*.daifu.ku" },
                Geolocation = new HashSet<string> { Source.None }
            });

            Assert.Equal("autoplay 'self' http://*.daifu.ku; geolocation 'none';", builder);
        }


        /// <summary>
        /// Set up rules for any feature
        /// </summary>
        /// <param name="featureName">Name of the feature you want rules for.
        /// The official list is at https://github.com/WICG/feature-policy/blob/master/features.md</param>
        public FeaturePolicyOtherFeatureBuilder AllowOtherFeature(string featureName)
        {
            var options = new FeaturePolicyOtherFeatureOptions(featureName);
            var builder = new FeaturePolicyOtherFeatureBuilder(options);
            _otherBuilders.Add(builder);
            return builder;
        }

        public FeaturePolicyOptions BuildFeaturePolicyOptions()
        {
            _options.Geolocation = AllowGeolocation.BuildOptions();
            _options.Midi = AllowMidi.BuildOptions();
            _options.Notifications = AllowNotifications.BuildOptions();
            _options.Push = AllowPush.BuildOptions();
            _options.SyncXhr = AllowSyncXhr.BuildOptions();
            _options.Microphone = AllowMicrophone.BuildOptions();
            _options.Camera = AllowCamera.BuildOptions();
            _options.Magnetometer = AllowMagnetometer.BuildOptions();
            _options.Gyroscope = AllowGyroscope.BuildOptions();
            _options.Speaker = AllowSpeaker.BuildOptions();
            _options.Vibrate = AllowVibrate.BuildOptions();
            _options.Fullscreen = AllowFullscreen.BuildOptions();
            _options.Payment = AllowPayment.BuildOptions();
            _options.Accelerometer = AllowAccelerometer.BuildOptions();
            _options.AmbientLightSensor = AllowAmbientLightSensor.BuildOptions();
            _options.Autoplay = AllowAutoplay.BuildOptions();
            _options.EncryptedMedia = AllowEncryptedMedia.BuildOptions();
            _options.PictureInPicture = AllowPictureInPicture.BuildOptions();
            _options.Usb = AllowUsb.BuildOptions();
            _options.Vr = AllowVr.BuildOptions();
            _options.Other = _otherBuilders.Select(b => b.BuildOptions()).ToDictionary(o => o.FeatureName);

            return _options;
        }


        /// <summary>
        /// Controls whether the current document is allowed to use the accelerometer sensor.
        /// If disabled then constructing of a Sensor-based interface object will throw a
        /// <code>SecurityError</code>. The events are not fired. If an interface (or an
        /// event) requires access to multiple sensors of different types than each of the
        /// corresponding sensor features must be allowed in order to use the interface.
        /// </summary>
        /// <returns>A configured <see cref="AccelerometerFeaturePolicyDirectiveBuilder"/></returns>
        public AccelerometerFeaturePolicyDirectiveBuilder AddAccelerometer() => AddDirective(new AccelerometerFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether the current document is allowed to use the ambient light sensor sensor.
        /// If disabled then constructing of a Sensor-based interface object will throw a
        /// <code>SecurityError</code>. The events are not fired. If an interface (or an
        /// event) requires access to multiple sensors of different types than each of the
        /// corresponding sensor features must be allowed in order to use the interface.
        /// </summary>
        /// <returns>A configured <see cref="AmbientLightSensorFeaturePolicyDirectiveBuilder"/></returns>
        public AmbientLightSensorFeaturePolicyDirectiveBuilder AddAmbientLightSensor() => AddDirective(new AmbientLightSensorFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls access to autoplay of media requested through the
        /// <code>HTMLMediaElement</code> interface. If disabled in a document,
        /// then calls to <code>play()</code> without a user gesture will
        /// reject the promise with a <code>NotAllowedError</code> DOMException
        /// object as its parameter. The autoplay attribute will be ignored.
        /// </summary>
        /// <returns>A configured <see cref="AutoplayFeaturePolicyDirectiveBuilder"/></returns>
        public AutoplayFeaturePolicyDirectiveBuilder AddAutoplay() => AddDirective(new AutoplayFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls access to video input devices requested through the
        /// NavigatorUserMedia interface. If disabled in a document, then calls
        /// to <code>getUserMedia()</code> will not grant access to video input
        /// devices in that document.
        /// </summary>
        /// <returns>A configured <see cref="CameraFeaturePolicyDirectiveBuilder"/></returns>
        public CameraFeaturePolicyDirectiveBuilder AddCamera() => AddDirective(new CameraFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether encrypted media extensions are available. If disabled
        /// The promise returned by <code>requestMediaKeySystemAccess()</code> must
        /// return a promise which rejects with a <code>SecurityError</code> DOMException
        /// object as its parameter.
        /// </summary>
        /// <returns>A configured <see cref="EncryptedMediaFeaturePolicyDirectiveBuilder"/></returns>
        public EncryptedMediaFeaturePolicyDirectiveBuilder AddEncryptedMedia() => AddDirective(new EncryptedMediaFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether the current document is allowed to use
        /// <code>Element.requestFullScreen()</code>. When this policy is enabled,
        /// the returned <code>Promise</code> rejects with a <code>TypeError</code>.
        /// </summary>
        /// <returns>A configured <see cref="FullscreenFeaturePolicyDirectiveBuilder"/></returns>
        public FullscreenFeaturePolicyDirectiveBuilder AddFullscreen() => AddDirective(new FullscreenFeaturePolicyDirectiveBuilder());


        /// <summary>
        /// Controls whether the current document is allowed to use the accelerometer sensor.
        /// If disabled then constructing of a Sensor-based interface object will throw a
        /// <code>SecurityError</code>. The events are not fired. If an interface (or an
        /// event) requires access to multiple sensors of different types than each of the
        /// corresponding sensor features must be allowed in order to use the interface.
        /// </summary>
        /// <returns>A configured <see cref="AccelerometerFeaturePolicyDirectiveBuilder"/></returns>
        public AccelerometerFeaturePolicyDirectiveBuilder AddAccelerometer() => AddDirective(new AccelerometerFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether the current document is allowed to use the ambient light sensor sensor.
        /// If disabled then constructing of a Sensor-based interface object will throw a
        /// <code>SecurityError</code>. The events are not fired. If an interface (or an
        /// event) requires access to multiple sensors of different types than each of the
        /// corresponding sensor features must be allowed in order to use the interface.
        /// </summary>
        /// <returns>A configured <see cref="AmbientLightSensorFeaturePolicyDirectiveBuilder"/></returns>
        public AmbientLightSensorFeaturePolicyDirectiveBuilder AddAmbientLightSensor() => AddDirective(new AmbientLightSensorFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls access to autoplay of media requested through the
        /// <code>HTMLMediaElement</code> interface. If disabled in a document,
        /// then calls to <code>play()</code> without a user gesture will
        /// reject the promise with a <code>NotAllowedError</code> DOMException
        /// object as its parameter. The autoplay attribute will be ignored.
        /// </summary>
        /// <returns>A configured <see cref="AutoplayFeaturePolicyDirectiveBuilder"/></returns>
        public AutoplayFeaturePolicyDirectiveBuilder AddAutoplay() => AddDirective(new AutoplayFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls access to video input devices requested through the
        /// NavigatorUserMedia interface. If disabled in a document, then calls
        /// to <code>getUserMedia()</code> will not grant access to video input
        /// devices in that document.
        /// </summary>
        /// <returns>A configured <see cref="CameraFeaturePolicyDirectiveBuilder"/></returns>
        public CameraFeaturePolicyDirectiveBuilder AddCamera() => AddDirective(new CameraFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether encrypted media extensions are available. If disabled
        /// The promise returned by <code>requestMediaKeySystemAccess()</code> must
        /// return a promise which rejects with a <code>SecurityError</code> DOMException
        /// object as its parameter.
        /// </summary>
        /// <returns>A configured <see cref="EncryptedMediaFeaturePolicyDirectiveBuilder"/></returns>
        public EncryptedMediaFeaturePolicyDirectiveBuilder AddEncryptedMedia() => AddDirective(new EncryptedMediaFeaturePolicyDirectiveBuilder());

        /// <summary>
        /// Controls whether the current document is allowed to use
        /// <code>Element.requestFullScreen()</code>. When this policy is enabled,
        /// the returned <code>Promise</code> rejects with a <code>TypeError</code>.
        /// </summary>
        /// <returns>A configured <see cref="FullscreenFeaturePolicyDirectiveBuilder"/></returns>
        public FullscreenFeaturePolicyDirectiveBuilder AddFullscreen() => AddDirective(new FullscreenFeaturePolicyDirectiveBuilder());

Joonasw.AspNetCore.SecurityHeaders.FeaturePolicy.Builder.FeaturePolicyBuilder : Object

Constructors :

public FeaturePolicyBuilder()

Methods :

public FeaturePolicyFeatureBuilder<FeaturePolicyGeolocationOptions> get_AllowGeolocation()
public FeaturePolicyFeatureBuilder<FeaturePolicyMidiOptions> get_AllowMidi()
public FeaturePolicyFeatureBuilder<FeaturePolicyNotificationsOptions> get_AllowNotifications()
public FeaturePolicyFeatureBuilder<FeaturePolicyPushOptions> get_AllowPush()
public FeaturePolicyFeatureBuilder<FeaturePolicySyncXhrOptions> get_AllowSyncXhr()
public FeaturePolicyFeatureBuilder<FeaturePolicyMicrophoneOptions> get_AllowMicrophone()
public FeaturePolicyFeatureBuilder<FeaturePolicyCameraOptions> get_AllowCamera()
public FeaturePolicyFeatureBuilder<FeaturePolicyMagnetometerOptions> get_AllowMagnetometer()
public FeaturePolicyFeatureBuilder<FeaturePolicyGyroscopeOptions> get_AllowGyroscope()
public FeaturePolicyFeatureBuilder<FeaturePolicySpeakerOptions> get_AllowSpeaker()
public FeaturePolicyFeatureBuilder<FeaturePolicyVibrateOptions> get_AllowVibrate()
public FeaturePolicyFeatureBuilder<FeaturePolicyFullscreenOptions> get_AllowFullscreen()
public FeaturePolicyFeatureBuilder<FeaturePolicyPaymentOptions> get_AllowPayment()
public FeaturePolicyFeatureBuilder<FeaturePolicyAccelerometerOptions> get_AllowAccelerometer()
public FeaturePolicyFeatureBuilder<FeaturePolicyAmbientLightSensorOptions> get_AllowAmbientLightSensor()
public FeaturePolicyFeatureBuilder<FeaturePolicyAutoplayOptions> get_AllowAutoplay()
public FeaturePolicyFeatureBuilder<FeaturePolicyEncryptedMediaOptions> get_AllowEncryptedMedia()
public FeaturePolicyFeatureBuilder<FeaturePolicyPictureInPictureOptions> get_AllowPictureInPicture()
public FeaturePolicyFeatureBuilder<FeaturePolicyUsbOptions> get_AllowUsb()
public FeaturePolicyFeatureBuilder<FeaturePolicyVrOptions> get_AllowVr()
public FeaturePolicyOtherFeatureBuilder AllowOtherFeature(String featureName = )
public FeaturePolicyOptions BuildFeaturePolicyOptions()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods

Other methods