zoukankan      html  css  js  c++  java
  • C# TOKEN的保存与验证

    Token主要为了防止非本页数据的提交,防止重复提交。

         /**
            * 
            * 保存TOKEN信息
            * 
            */
            public void saveToken()
            {
                //此处生成md5串
                string md5 = getMD5(token_id);
                this.ViewData[Constant.TOKEN_ID_FORM] = "<input type="hidden" name="" + Constant.TOKEN_ID_FORM + "" value="" + md5 + "">";
                this.Session[Constant.TOKEN_ID_FORM] = md5;
            }
    
            /**
            * 
            * 验证FORM 内的TOKEN信息 是否与SESSION的信息一致 
            * 
            */
            public bool isValidateToken()
            {
                string md5 = this.Session[Constant.TOKEN_ID_FORM] as string;
    
                string md5_form = this.read(Constant.TOKEN_ID_FORM);
    
                if (string.IsNullOrEmpty(md5))
                {
                    return false;
                }
                else
                {
                    if (md5.Equals(md5_form))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
    
            /**
             * 
             * 对数据进行加密,生成32位的16进制字符串
    
             * 
             */
            public static string getMD5(string strSource)
            {
                string strResult = "";
                try
                {   
                    //Create
                    System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
    
                    //注意编码UTF8、UTF7、Unicode等的选择 
                    byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource));
    
                    //字节类型的数组转换为字符串
    
                    for (int i = 0; i < bytResult.Length; i++)
                    {
                        //16进制转换 
                        string temps = bytResult[i].ToString("x");
                        if (temps.Length == 1)
                        {
                            temps = "0" + temps;
                        }
                        strResult = strResult + temps;
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("MD5加密算法错误!错误信息:" + ex.Message);
                }
                return strResult.Substring(8,16);
            }

    调用:

            //跳转的前一个页面,生成Token
                this.saveToken();
    
                //保存页面 验证Token
                if (this.isValidateToken())
                {
                    //重新设置token
                    this.saveToken();
                }
  • 相关阅读:
    ZOJ 2158 Truck History
    Knight Moves (zoj 1091 poj2243)BFS
    poj 1270 Following Orders
    poj 2935 Basic Wall Maze (BFS)
    Holedox Moving (zoj 1361 poj 1324)bfs
    ZOJ 1083 Frame Stacking
    zoj 2193 Window Pains
    hdu1412{A} + {B}
    hdu2031进制转换
    openjudge最长单词
  • 原文地址:https://www.cnblogs.com/cang12138/p/5502192.html
Copyright © 2011-2022 走看看