.net Core前后端安全性

作者:秋名
撰写时间:2020 年 9 月 19 日
技术要点:预防前端通过路径传入非法字符到后端,导致数据奔溃,在后台使用try{}catch{}进行捕获非法字符。
使用前端Vue+后台.Net Core3.0+vs2019+SQLserver

  methods: {
    getProductById(pid) {
      var thisVue = this;
      this.$http
        .get("https://localhost:44310/api/Products/getProductById/?id=" + pid)
        .then(function (res) {
          console.log(res.status)
          if(res.status==205){//当接收status值为205,将跳转至其他页面。
              thisVue.$router.push("/ErrPage");
          }
          thisVue.product = res.data;
        });
    },
public Products GetProductById(string id) {
            //为了方法容措性更好 方法后的参数建议string类型
            int pid = -1;
            try
            {
               pid= Convert.ToInt32(id);
            }
            catch (Exception)
            {
                //HTTP响应状态码 ,返回页面 403(拒绝访问)
                HttpContext.Response.StatusCode = 205;
                return default;//返回空
                throw;
            }
            List<Products> productList = Products.GetProductList();
            return productList.Find(m => m.Id == pid);
        }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值