C# Attribute应用:类签名
在应用别人接口的时候,总是要用签名,很是不理解签名这是怎么知道做的。通过对Attribute的学习了解。大体可以用Attribute来做签名应用。
具体过程如下:
首先我们要先定义一个类,该类继承Attribute。该类主要最用是,签名需要用到的方法、参数和获取加密文件
public class CashiSongAttribute : Attribute
{
/// <summary>
/// 签名参数
/// </summary>
public string[] Param { get; set; }
/// <summary>
/// 是否签名
/// </summary>
public bool IsSign { get; set; }
/// <summary>
/// 加密文件
/// </summary>
/// <param name="bp"></param>
/// <param name="mi"></param>
/// <returns></returns>
public string ParamEncryption(BasePage bp,System.Reflection.MethodInfo mi)
{
if (Param != null && Param.Length > )
{
string md5 = "op" + mi.Name.ToLower();
foreach (string item in Param)
{
if (item.ToLower() == "op" || item.ToLower() == "sign")
continue;
md5 += item + bp.GetRequest(item);
}
byte[] bytestr = Encoding.Default.GetBytes(md5);
MD5 _md5 = new MD5CryptoServiceProvider();
byte[] bytesend = _md5.ComputeHash(bytestr);
return BitConverter.ToString(bytesend).Replace("-", "");
}
return "";
}
}
新建一个页面,在该页面创建一个方法,并加入该特性
[CashiSong(IsSign = true, Param = new string[] { "op", "name" })]
public string getceshicon()
{
return "签名成功!";
}
下面关键就再也通过调用方式的时候,验证参数是否都符合,加密文件是否正确。
创建一个基类BasePage,该基类主要负责,接受参数,并指定参数指定的方法,并判断签名信息是否正确。
这里会用到:System.Reflection.MethodInfo的应用、获取特性Attribute参数内容。
public class BasePage : Page
{
public BasePage()
{
this.Load += new EventHandler(BasePage_Load);
} void BasePage_Load(object sender, EventArgs e)
{
Response.AddHeader("Accept-Charset","UTF-8");
string op = GetRequest("op");
if (!string.IsNullOrEmpty(op))
{
System.Reflection.MethodInfo mi = this.GetType().GetMethod(op);
Attribute_Jude(mi);
}
this.Response.End();
}
/// <summary>
/// 签名判断
/// </summary>
/// <param name="mi"></param>
public void Attribute_Jude(MethodInfo mi)
{
MsgModel Msg = new MsgModel();
if (mi.IsDefined(typeof(CashiSongAttribute), false))
{
object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false);
CashiSongAttribute iplimit = (CashiSongAttribute)attrs[];
object responsestr=null;
if (iplimit != null && iplimit.Param.Length > )
{
string server_sign = GetRequest("sign");
string client_sign = iplimit.ParamEncryption(this, mi);
if (!server_sign.Equals(client_sign, StringComparison.OrdinalIgnoreCase)&&iplimit.IsSign)
{
Msg.msg = "Sing Error";
Msg.toile = ;
Send(Msg);
return;
}
responsestr = mi.Invoke(this, null);
}
Msg.toile = ;
Msg.msg = responsestr.ToString();
Send(Msg);
}
}
public void Send(MsgModel Msg)
{
Response.AddHeader("Content-type","applictaion/json");
JavaScriptSerializer javaScript = new JavaScriptSerializer();
string Con = javaScript.Serialize(Msg);
Response.Write(Con);
}
public string GetRequest(string key)
{
if (Request.QueryString[key] == null)
return "";
else
return Request.QueryString[key];
}
} public class MsgModel
{
public string msg { get; set; }
public int toile { get; set; }
}
获取特性参数内容的方法(CashiSongAttribute 为自定义特性类)
if (mi.IsDefined(typeof(CashiSongAttribute), false))
{
object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false);
CashiSongAttribute iplimit = (CashiSongAttribute)attrs[];
}
C# Attribute应用:类签名的更多相关文章
- C# System.Attribute(验证类)
本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据. 在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...
- IDEA配置类签名
- 使用TypeDescriptor给类动态添加Attribute
给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...
- 使用TypeDescriptor给类动态添加Attribute【转】
源文 : http://www.cnblogs.com/bicker/p/3326763.html 给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overf ...
- MSIL实用指南-给字段、属性、方法、类、程序集加Attribute
C#编程中可以给字段.方法.类以及程序集加特性即继承于Attribute的类.这里讲解怎么在IL中给它们加上特性. 生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyB ...
- 理解Attribute
注:本文转载自http://kb.cnblogs.com/page/87531/ Attribute与Property 的翻译区别 Attribute 一般译作“特性”,Property 仍然译为“属 ...
- AOP 面向切面编程, Attribute在项目中的应用
一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
- Attribute
Attribute介绍 咱们来说Attribute,他是一个类,所以自定义的Attribute都是继承自System.Attribute,一般命名的时候都是以Attribute结尾.在使用的时候我们可 ...
- 通过声明Attribute属性改变不同类的输出效果
ConsoleApplication--控制台应用程序 首先创建基类: using System; using System.Collections.Generic; using System.Lin ...
随机推荐
- 题解 BZOJ 1002 【[FJOI2007]轮状病毒】
题目链接 emm…… 正解:矩阵树定理,但是本宝宝不会求基尔霍夫矩阵. 开始考场方法: 手动模拟$n=1--5$时的答案(数不大,~~画画就出来了~~要画上半个小时). 画出来,答案是这样的:$1$ ...
- bzoj2395 [Balkan 2011]Timeismoney(最小乘积生成树+计算几何)
题意 每条边有两个权值\(c,t\),请求出一颗生成树,使得\(\sum c\times \sum t\)最小 题解 为什么生成树会和计算几何扯上关系-- 对于每棵树,设\(x=c,y=t\),我们可 ...
- Shell等,不等......
-eq //等于 -ne //不等于 -gt //大于 (greater ) -lt //小于 (less) -g ...
- Flink学习笔记:Operators之Process Function
本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...
- string[] 转换为 int[]
string[] ke=...... int[] output = Array.ConvertAll<string, int>(ke,delegate (string s) { retur ...
- Maven搭建Spring+SpringMVC+Mybatis+Shiro项目详解
一. 环境搭建: 1. 开发工具:myeclipse 2014 / IDEA: 2. maven管理版本:apache-maven-3.0+: 3. jdk 1.7.0+4. Tomcat8.0 二: ...
- C++_异常3-异常机制throw try catch
下面介绍如何使用异常机制来处理错误. C++异常是对程序运行过程中发生的异常情况的一种响应. 异常提供了将控制权从程序的一部分传递到另一部分的途径. 对异常的处理有3个组成部分: 1)引发异常 -- ...
- 洛谷 P3157 [CQOI2011]动态逆序对(树套树)
题面 luogu 题解 树套树(树状数组套动态开点线段树) 静态使用树状数组求逆序对就不多说了 用线段树代替树状数组,外面套树状数组统计每个点逆序对数量 设 \(t1[i]\)为\(i\)前面有多少个 ...
- JAVA 大数 A+B问题
A + B Problem II I have a very simple problem for you. Given two integers A and B, your job is to ca ...
- Win10如何新建用户怎么添加新账户
https://jingyan.baidu.com/article/25648fc162d5899190fd0069.html 很多朋友都是安装完Windows10系统后,直接使用超级管理员账号登录系 ...