/// <summary>
        /// Invoke the middleware
        /// </summary>
        /// <param name="context">The current context</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (_mustGenerateNonce)
            {
                context.SetNonce(_nonceGenerator.GetNonce(Constants.DefaultBytesInNonce));
            }

            context.Response.OnStarting(OnResponseStarting, Tuple.Create(this, context, _policy));
            await _next(context);
        }
Exemple #2
0
    /// <summary>
    /// Invoke the middleware.
    /// </summary>
    /// <param name="context">The current HttpContext</param>
    public Task Invoke(HttpContext context)
    {
        context.EnsureNotNull(nameof(context));

        var nonce = _nonceGenerator.GetNonce();

        context.SetNonce(nonce);

        var builder = new CspOptionsBuilder(nonce);

        _configure(builder);
        var options = builder.Build();

        var header = new CspHeader(options);

        context.Response.Headers.Add(header.Key, header.Value);
        return(_next(context));
    }