一个页面跳转到另外一个页面直接将参数写在URL上面并不安全比如 http://XXXXXXXXXXX/meeting/shakeGroup?id=5381&uid=o0En_sj1J0bFgIBMPG37WjWMXpqY

参数id和uid需要进行加密,写个简单的例子来实现:

加密类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web; namespace CnbLogsProject.Util
{
public class EnCodeHelper
{
// url传输参数加密密钥
public static string strKeys = "abdfajrtrgjfg"; #region 加密字符串
/// <summary>
/// 加密
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static string GetEncryption(string strValue)
{
//加密标准算法的对象
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
//建立加密对象的密钥和偏移量
provider.Key = Encoding.ASCII.GetBytes(strKeys.Substring(, ));
//原文使用Encoding.ASCII方法的GetBytes方法
provider.IV = Encoding.ASCII.GetBytes(strKeys.Substring(, ));
//将要加密的字符放到byte数组中
byte[] bytes = Encoding.UTF8.GetBytes(strValue);
//输入的文本必须是英文文本
MemoryStream stream = new MemoryStream();
//定义将数据连接到加密转换的流
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, , bytes.Length);//将当前字节写入到流中
stream2.FlushFinalBlock();//清除缓存区
StringBuilder builder = new StringBuilder();
//循环遍历每个字节
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();//关闭释放资源
return builder.ToString();
}
#endregion #region 解密字符串
/// <summary>
/// 解密
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static string GetDecryption(string strValue)
{
//解密标准算法的对象
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
//建立解密密对象的密钥和偏移量
provider.Key = Encoding.ASCII.GetBytes(strKeys.Substring(, ));
//原文使用Encoding.ASCII方法的GetBytes方法
provider.IV = Encoding.ASCII.GetBytes(strKeys.Substring(, ));
//将要解密的字符放到byte数组中
byte[] buffer = new byte[strValue.Length / ];
//循环遍历遍历
for (int i = ; i < (strValue.Length / ); i++)
{
int num2 = Convert.ToInt32(strValue.Substring(i * , ), 0x10);
buffer[i] = (byte)num2;
}
//输入的文本必须是英文文本
MemoryStream stream = new MemoryStream();
//定义将数据连接到解密转换的流
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
//将当前字节写入到流中
stream2.Write(buffer, , buffer.Length);
stream2.FlushFinalBlock();//清除缓存区
stream.Close();//关闭释放资源
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}
#endregion
}
}
strKeys 为秘钥可以写在配置文件里面

控制器(将A页面的参数加密后暴露给客户端跳转到B页面时候解密):
     /// <summary>
/// A页面
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
string id = "";
string uid = "o0En_sj1J0bFgIBMPG37WjWMXpqY";
id = EnCodeHelper.GetEncryption(id);
uid = EnCodeHelper.GetEncryption(uid); ViewBag.id = id;
ViewBag.uid = uid;
return View();
} /// <summary>
/// B页面
/// </summary>
/// <param name="id"></param>
/// <param name="uid"></param>
/// <returns></returns>
public ActionResult Home(string id="",string uid="")
{
ViewBag.id =EnCodeHelper.GetDecryption(id);
ViewBag.uid =EnCodeHelper.GetDecryption(uid);
return View();
}

视图:

@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<button id="re">跳转</button>
<script> $(function () { $("#re").click(function () {
location.href = "Home?id="+"@ViewBag.id"+"&uid="+"@ViewBag.uid";
});
});
</script>
@{
ViewBag.Title = "Home";
} <input value="@ViewBag.id" />
<input value="@ViewBag.uid"/>
<h2>Home</h2>

效果:

原来的URL:http://localhost:63792/Home/Home?id=282D147B1B12BAE3&uid=29732D957DD4EF753BC3E94797D1018D230457174ABD43EF1ED2FEA651E8351E

跳转到B页面后成功解密:

对应上我们开头的

http://XXXXXXXXXXX/meeting/shakeGroup?id=5381&uid=o0En_sj1J0bFgIBMPG37WjWMXpqY

参数id和uid需要进行加密,写个简单的例子来实现:

当然还有其他很多方法

ASP.MVC当URL跳转时候参数的安全性的更多相关文章

  1. JS分页 + 获取MVC地址栏URL路径的最后参数

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  2. MVC中URL传多个参数

    1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...

  3. url跳转路径参数获取

    function getUrlParam1(name){ //正则表达式过滤 var reg = new RegExp("(^|&)" + name + "=([ ...

  4. 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数

    目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...

  5. 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action

    目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...

  6. C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)

    C# MVC 用户登录状态判断   来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...

  7. ASP.NET MVC制作404跳转(非302和200)

    前言:距离上次发文已经有几个月了! 这段时间李,制作了一个博客网站,现将博客文章选一些发表到博客园,顺便为自己网站打一下广告! 产生404的原因主要有以下: 1.浏览器和爬虫:某些浏览器会请求网站的f ...

  8. 【转载】ASP.NET MVC重写URL制作伪静态网页,URL地址以.html结尾

    在搜索引擎优化领域,静态网页对于SEO的优化有着很大的好处,因此很多人就想把自己的网站的一些网页做成伪静态.我们现在在网络上发现很多博客网站.论坛网站.CMS内容管理系统等都有使用伪静态这一种情况,伪 ...

  9. ASP.NET Core MVC中URL和数据模型的匹配

    Http GET方法 首先我们来看看GET方法的Http请求,URL参数和ASP.NET Core MVC中Controller的Action方法参数匹配情况. 我定义一个UserController ...

随机推荐

  1. 转:ef获取某个表中的部分字段值

    我有个新闻表 id,title,body,createtime,author,click 使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上. public static ...

  2. SQL系统函数的使用(实验五)

    SQL系统函数的使用(试验5) 函数在查询语句中的使用 查询员工的姓名和日工资(保留1位小数): 查询并显示部门号为01和02的所有员工的姓名首字及岗位: 查询并显示所有员工的姓名及工龄: 查询199 ...

  3. hadoop启动name失败

    namenode失败十分的常见, 1.java.io.EOFException; Host Details : local host is: "hadoop1/192.168.41.134& ...

  4. oracle数据库管理系统常见的错误(二)

    oracle数据库,对于新手来说总会遇到这样的问题: 相信大家都遇到了这样的问题,说实话,我曾经就遇到过这样的问题,但是不好意思问旁边的技术大咖,都有点怀疑人生了,然后自己在网上去查找原因,结果发现, ...

  5. mysql timeout

    (待更新整理) 因为最近遇到一些超时的问题,正好就把所有的timeout参数都理一遍,首先数据库里查一下看有哪些超时: root@localhost : test 12:55:50> show ...

  6. vue-cli 如何配置sass

    第一步:安装对应的node模块 npm install node-sass --save-dev npm install sass-loader --save-dev 第二步:在webpack.bas ...

  7. Cubieboard2安装Fedora20

    几天前入手一块Cubieboard2,又买了张16G的TF卡,装个linux折腾折腾.以前都是在虚拟机上用linux,个人比较喜欢Fedora,因为总能用上最新版的软件,像支持C++11的GCC.Cl ...

  8. EntityFramework6与EntityFrameworkCore的区别

    EntityFramework6 EF6 是一个久经考验的数据库访问技术,发展多年,拥有许多特性,并且成熟稳定.2008年EF作为 .Net 3.5 Sp1 和Visual Studio 2008 S ...

  9. 谷歌浏览器 插件安装配置Momentum chrome

    总之一句话就是这个Momentum插件可以把你的谷歌弄的漂亮一些,来搞一波 下载地址 http://www.cnplugins.com/down/predownnew.aspx?id=33842 下载 ...

  10. C语言之for循环

    #include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int i; for(i=1;i< ...