首页 > 编程 > .NET > 正文

ASP.NET MVC结合JavaScript登录、校验和加密

2024-07-10 12:46:48
字体:
来源:转载
供稿:网友

最近闲来无事给自己写了家庭财务收支管理系统,也就包含支出管理,收入管理和一些统计功能。

先说登录模块,因为涉及GET和POST请求,这些东西都是能被监控和抓取的所以就考虑这使用RSA加密解密方式传输用户名和密码参数,页面JS如下: 

/*需要引入三个JS文件,BigInt.js、RSA.js和Barrett.js,用到cookie则需要引入jquery.cookie.js文件*///与后台交互获取公钥function getPublicKey() {  var pubKey = '';  if ($.cookie('publicKey') == null) {    $.ajax({      url: "/Account/GetRsaPublicKey",      type: "get",      contentType: "application/x-www-form-urlencoded; charset=utf-8",      async: false,      data: {},      dataType: "json",      success: function (data) {        if (data.Code == 0) {          pubKey = data.RsaPublicKey + "," + data.Key;          $.cookie('publicKey', pubKey, { expires: 1 / 1440 });        } else {          Config.Method.JudgeCode(data, 1);        }      }    });  } else {    pubKey = $.cookie('publicKey');  }  return pubKey;}//公钥加密用户密码Pwd为RSA加密后参数function rsaEncrypt(pwd) {  var publicKey = getPublicKey();  setMaxDigits(129);  var rsaKey = new RSAKeyPair(publicKey.split(",")[0], "", publicKey.split(",")[1]);  var pwdRtn = encryptedString(rsaKey, pwd);  return pwdRtn + "," + publicKey.split(",")[2];}//POST登录请求,参数<script type="text/javascript">  $(function () {    $('#btnSubmit').live('click', function () {      var uName = $('#u').val();      var pwd = $('#p').val();      if (uName == '') {        alert('用户名不能为空');        return;      }      if (pwd == '') {        alert('用户密码不能为空');        return;      }      var enPwd = rsaEncrypt(pwd);      $.ajax({        type: "POST",        url: "/Account/UserLogin",        data: { 'UserName': uName, 'Pwd': enPwd.split(",")[0], 'Key': enPwd.split(",")[1], 'RUrl': $('#hiddenUrl').val() },        contentType: "application/x-www-form-urlencoded; charset=utf-8",        async: false,        dataType: "json",        success: function (data) {          if (data.result == true) {            window.location.href = data.url;            return false;          } else {            $('#msg').text(data.message);          }        },        error: function (XMLHttpRequest, textStatus, errorThrown) {          $('#msg').text(XMLHttpRequest.status + '||' + XMLHttpRequest.readyState + '||' + textStatus);        }      });    });  })</script>

前台加密完成后就需要后台做解密处理,解密完成后需要使用MD5加密现有密码与数据库中用户密码进行比较验证,如果验证通过则需要写入cookie以便下次用户能自   动登录,由于cookie中我不希望用户名和密码都明码存储,我这里用到了AES加密的方式,自定义一个32位的加密密钥对cookie进行加密解密处理,后台c#代码如  下: 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表