当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core下给网站做安全设置的方法详解

asp.net core下给网站做安全设置的方法详解

2018年08月08日  | 移动技术网IT编程  | 我要评论

谈恋爱短信,逃花园记,北京最新楼市

前言

本文主要介绍了关于asp.net core给网站做安全设置的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

设置方法如下

首先,我们来看下stack overflow网站的请求头文件:


可以看到一些我们熟悉或是陌生的http头部文件字段。

在这里我们在对http输入流的头部文件中,做一些基本的防护。首先要明确,既然我们是对http头部做处理,那么就需要在startup.cs类的

configuration方法中做处理,因为这里就是处理http输入流的。

首先做一些基本的处理,比如中间件和基本的类:

public class securityheaderspolicy 
{
 public idictionary<string, string> setheaders { get; }
  = new dictionary<string, string>();
 
 public iset<string> removeheaders { get; }
 = new hashset<string>();
}

这里的头部信息是我们定义好的,用来增加或是删除头部信息,然后就是我们的中间件:

public class securityheadersmiddleware 
{
 private readonly requestdelegate _next;
 private readonly securityheaderspolicy _policy;

 public securityheadersmiddleware(requestdelegate next, securityheaderspolicy policy)
 {
 _next = next;
 _policy = policy;
 }

 public async task invoke(httpcontext context)
 { 
 iheaderdictionary headers = context.response.headers;

 foreach (var headervaluepair in _policy.setheaders)
 {
  headers[headervaluepair.key] = headervaluepair.value;
 }

 foreach (var header in _policy.removeheaders)
 {
  headers.remove(header);
 }

 await _next(context);
 }
}

基于iapplicationbuilder接口做一个中间件的扩展方法:

public static class middlewareextensions 
{
 public static iapplicationbuilder usesecurityheadersmiddleware(this iapplicationbuilder app, securityheadersbuilder builder)
 {
 securityheaderpolicy policy = builder.build();
 return app.usemiddleware<securityheadersmiddleware>(policy);
 }
}

封装好相关的安全类:

public class securityheadersbuilder 
{
 private readonly securityheaderspolicy _policy = new securityheaderspolicy();

 public securityheadersbuilder adddefaultsecurepolicy()
 {
 addframeoptionsdeny();
 addxssprotectionblock();
 addcontenttypeoptionsnosniff();
 addstricttransportsecuritymaxage();
 removeserverheader();

 return this;
 }

 public securityheadersbuilder addframeoptionsdeny()
 {
 _policy.setheaders[frameoptionsconstants.header] = frameoptionsconstants.deny;
 return this;
 }

 public securityheadersbuilder addframeoptionssameorigin()
 {
 _policy.setheaders[frameoptionsconstants.header] = frameoptionsconstants.sameorigin;
 return this;
 }

 public securityheadersbuilder addframeoptionssameorigin(string uri)
 {
 _policy.setheaders[frameoptionsconstants.header] = string.format(frameoptionsconstants.allowfromuri, uri);
 return this;
 }

 public securityheadersbuilder removeserverheader()
 {
 _policy.removeheaders.add(serverconstants.header);
 return this;
 }

 public securityheadersbuilder addcustomheader(string header, string value)
 {
 _policy.setheaders[header] = value;
 return this;
 }

 public securityheadersbuilder removeheader(string header)
 {
 _policy.removeheaders.add(header);
 return this;
 }

 public securityheaderspolicy build()
 {
 return _policy;
 }
}

最后注入到http的输入流中:

app.usesecurityheadersmiddleware(new securityheadersbuilder() 
.adddefaultsecurepolicy()
);

然后我们浏览一下网页,就可以在http的头部信息中看到:

http/1.1 200 ok 
content-type: text/html; charset=utf-8 
x-frame-options: deny 
x-xss-protection: 1; mode=block 
x-content-type-options: nosniff 
strict-transport-security: max-age=31536000 
x-powered-by: asp.net

还有一个就是csrf的防护,如果之前你用过asp.net mvc,在最基本的mvc模板中,可能你会留意到已有的cshtml页面中的form表单有这么一句:

@html.antiforgerytoken()

这就是微软在mvc框架中为我们提供的防护csrf的方法。我们在表单中直接使用上面那句代码就可以了,然后在表单提交的action方法中:

[validateantiforgerytoken]
[httppost]
public iactionresult antiform(string message)
{
return content(message);
}

使用[validateantiforgerytoken]属性,来验证csrf。

参考链接:

代码地址:

https://github.com/ryanovo/aspnetcore-fileup-demo

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网