ติดตั้ง AdamBarclay.AspNetCore.SecurityHeaders แพ็คเกจ NUGET
Install-Package AdamBarclay.AspNetCore.SecurityHeadersเพื่อรวมมิดเดิลแวร์ส่วนหัวความปลอดภัยในไปป์ไลน์ ASP.NET ในระหว่างการกำหนดค่าแอปพลิเคชันรวมถึง:
using AdamBarclay . AspNetCore . SecurityHeaders ;และโทร:
app . UseSecurityHeaders ( ) การโทร app.UseSecurityHeaders() เป็น eqivalent ที่โทร:
app . UseSecurityHeaders (
c =>
{
c . ContentSecurityPolicy ( o =>
{
o . ConfigureDefault ( ) . Self ( ) ;
o . ConfigureObject ( ) . None ( ) ;
o . ConfigureDirective ( "frame-ancestors" ) . None ( ) ;
} ) ;
c . FrameOptions ( o => o . Deny ( ) ) ;
c . ReferrerPolicy ( o => o . StrictOriginWhenCrossOrigin ( ) ) ;
c . StrictTransportSecurity ( o => o . MaxAge ( TimeSpan . FromDays ( 365 ) ) . IncludeSubdomains ( ) ) ;
} ) ; โดยค่าเริ่มต้นส่วนหัวความปลอดภัยทั้งหมดจะรวมอยู่ด้วย หากต้องการปิดการใช้งานส่วนหัวใด ๆ ให้โทร Disable() บนตัวสร้างการกำหนดค่าส่วนหัวนั้น
app . UseSecurityHeaders (
c =>
{
c . ContentSecurityPolicy ( o => o . Disable ( ) ) ;
c . ContentTypeOptions ( o => o . Disable ( ) ) ;
c . FrameOptions ( o => o . Disable ( ) ) ;
c . ReferrerPolicy ( o => o . Disable ( ) ) ;
c . StrictTransportSecurity ( o => o . Disable ( ) ) ;
} ) ; ค่าเริ่มต้นสำหรับ content-security-policy คือ default-src 'self';frame-ancestors 'none';object-src 'none'
ค่าเริ่มต้นสำหรับ x-content-type-options คือ nosniff
ไม่สามารถกำหนดค่าค่าอื่นได้
ค่าเริ่มต้นสำหรับ x-frame-options จะ deny
ใช้ตัวสร้างการกำหนดค่า FrameOptions() เพื่อกำหนดค่าค่า
โทร Deny() เพื่อตั้งค่าให้ deny
app . UseSecurityHeaders ( c => c . FrameOptions ( o => o . Deny ( ) ) ) ; เรียก SameOrigin() เพื่อตั้งค่าเป็น sameorigin
app . UseSecurityHeaders ( c => c . FrameOptions ( o => o . SameOrigin ( ) ) ) ; ค่าเริ่มต้นสำหรับ referrer-policy คือ strict-origin-when-cross-origin
ค่าเริ่มต้นสำหรับ strict-transport-security คือ max-age=31536000;includeSubdomains