Umbraco 13 to 17 Program.CS

Hi,

I am trying to upgrade one of our sites, the oroginal developers added this to program.

app.Use(async (context, next) =>
{

context.Response.OnStarting(() =>
{
    var isUmbraco = context.Request.IsBackOfficeRequest();

    if (isUmbraco == false)
    {

    //string nonceValue = "approach1";

        // public web page
        // apply standard csp and nonce
        //var rng = RandomNumberGenerator.Create();
        //var nonceBytes = new byte[32];
        //rng.GetBytes(nonceBytes);
        //var nonce = Convert.ToBase64String(nonceBytes);      
        string csp = customSection.GetValue<string>("Content-Security-Policy");
        csp = csp.Replace("{injectMe}", nonce);
        context.Items.Add("ScriptNonce2", nonce);
        context.Response.HttpContext.Items.Add("ScriptNonce1", nonce);
        context.Response.Headers.Add("Content-Security-Policy", csp);

    }
    else
    {
        // it's umbraco, apply less strict csp
        string csp = customSection.GetValue<string>("Content-Security-Policy-Umbraco").ToString();
        context.Response.Headers.Add("Content-Security-Policy", new[] { csp });

    }
    return Task.FromResult(0);
});

await next();

}

I can’t work out how to get this in to 17. It keeps generating an error at await next().

Any pointers on how to fix this.

Thanks

Darren

Hi @darrenhunterEmerald

That is just running some extra middleware to add CSP headers. What error are you getting?

Justin

It a compile error.

Hi @darrenhunterEmerald ,

Please change line 64 from } to });

Also on line 63 to await next(context);

Adding some info on what’s going wrong:
For line 63: We need to pass the current context so that it can hand the request off to the next piece of middleware. Calling await next(); empty leaves it without the context it needs to continue.

For line 64: The errors CS1003 (',' expected) and CS1026 (')' expected) are cascading because the app.Use(...) method call was never properly closed. You closed the async lambda function with the brace }, but you still need the parenthesis and semicolon ); to finish the app.Use statement.

Regards,
Shekhar

Thank you.

@ShekharTarare beat me to it! Yes, missing bracket!