Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the below error while running the application

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/ 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' 'sha256-5uIP+HBVRu0WW8ep6d6+YVfhgkl0AcIabZrBS5JJAzs='". Either the 'unsafe-inline' keyword, a hash ('sha256-4Su6mBWzEIFnH4pAGMOuaeBrstwJN4Z3pq/s1Kn4/KQ='), or a nonce ('nonce-...') is required to enable inline execution.

Below is the code currently I am using

What I have tried:

const string modernizrHash1 = "sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=";
const string modernizrHash2 = "sha256-5uIP+HBVRu0WW8ep6d6+YVfhgkl0AcIabZrBS5JJAzs=";
app.UseCsp(options => options
.DefaultSources(s => s.Self())
.ScriptSources(s => s.Self().CustomSources("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/"))
.StyleSources(s => s.Self().CustomSources("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/", modernizrHash1, modernizrHash2))
.FontSources(s => s.Self().CustomSources("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/fonts/"))
.ImageSources(s => s.Self().CustomSources("data:"))
);
Posted
Updated 29-May-19 3:44am
v2
Comments
MadMyche 29-May-19 9:38am    
I think the problem you are having is more Content-Security-Policy and Modernizr related and not so much Net=Core. I am adding these to your question's tags.

It looks like this may be Modernizr not getting along with the sites Content-Security-Policy. Seems to me that you are not the only one experiencing this:

Modernizr Causes Content Security Policy (CSP) Violation Errors · Issue #1450 · Modernizr/Modernizr · GitHub[^]

Possible workaround
Content Security Policy restrictions workaround by termi · Pull Request #1263 · Modernizr/Modernizr · GitHub[^]

More on CSP
Content Security Policy (CSP) - HTTP | MDN[^]
 
Share this answer
 
Quote:
Either the 'unsafe-inline' keyword, a hash ('sha256-4Su6mBWzEIFnH4pAGMOuaeBrstwJN4Z3pq/s1Kn4/KQ='), or a nonce ('nonce-...') is required to enable inline execution.

Neither of the two hashes you've added to your CSP match the inline <style> content you're trying to load.

Generate a hash for the inline stylesheet, and add it to your CSP.

Report URI: CSP Hash Generator[^]
 
Share this answer
 
Comments
Nityananda Das 30-May-19 3:58am    
I have tried with new hash key but it doesn't work.
Richard Deeming 30-May-19 6:09am    
Yes, looks like it could be the issue that MadMyche found in solution 1. Have you tried the workaround from the link he posted?
Nityananda Das 31-May-19 2:28am    
I have fixed the issue by changing the hash keys shown in the browsers console error, I have replaced the console error hash key with my code hash key, but not sure whether this is the permanent solution or not.

Note: There is no issue when i am running it in IE browser, only in the chrome and firefox i am getting the issue.

Could you please guide me how the hash keys are getting generated and how i can fix this permanently ?
Nityananda Das 31-May-19 2:29am    
I have fixed the issue by changing the hash keys shown in the browsers console error, I have replaced the console error hash key with my code hash key, but not sure whether this is the permanent solution or not.

Note: There is no issue when i am running it in IE browser, only in the chrome and firefox i am getting the issue.

Could you please guide me how the hash keys are getting generated and how i can fix this permanently
Richard Deeming 31-May-19 6:36am    
You don't get the issue in IE because it doesn't support CSP properly, so it just ignores the restrictions.

Unfortunately, until the Modernizr team fix the problem, the only robust solution would be to allow "unsafe-inline" styles. You will obviously need to evaluate whether this is an acceptable risk for your site.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900