在应用别人接口的时候,总是要用签名,很是不理解签名这是怎么知道做的。通过对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应用:类签名的更多相关文章

  1. C# System.Attribute(验证类)

    本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据.  在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...

  2. IDEA配置类签名

  3. 使用TypeDescriptor给类动态添加Attribute

    给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...

  4. 使用TypeDescriptor给类动态添加Attribute【转】

    源文 : http://www.cnblogs.com/bicker/p/3326763.html 给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overf ...

  5. MSIL实用指南-给字段、属性、方法、类、程序集加Attribute

    C#编程中可以给字段.方法.类以及程序集加特性即继承于Attribute的类.这里讲解怎么在IL中给它们加上特性. 生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyB ...

  6. 理解Attribute

    注:本文转载自http://kb.cnblogs.com/page/87531/ Attribute与Property 的翻译区别 Attribute 一般译作“特性”,Property 仍然译为“属 ...

  7. AOP 面向切面编程, Attribute在项目中的应用

    一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...

  8. Attribute

    Attribute介绍 咱们来说Attribute,他是一个类,所以自定义的Attribute都是继承自System.Attribute,一般命名的时候都是以Attribute结尾.在使用的时候我们可 ...

  9. 通过声明Attribute属性改变不同类的输出效果

    ConsoleApplication--控制台应用程序 首先创建基类: using System; using System.Collections.Generic; using System.Lin ...

随机推荐

  1. javascript 数组排序

    var arr=[1,2,3,5,10,4,2,19,2,0]; alert(arr);//[1,2,3,5,10,4,2,19,2,0] arr.sort(function (a, b) {//升序 ...

  2. 从HDU1004来看C++<map>

    最近做题有点浮躁~于是从HDU第一题开始刷,刷到1004题的时候突发奇想来复习一发很久没有用到过的map 题意是给你n个气球,每个气球有一种颜色,让你求出数量最多的气球颜色 因为颜色是气球数量的识别关 ...

  3. 【spring boot】FilterRegistrationBean介绍

    前言 以往的javaee配置过滤器是在web.xml中配置的,如下代码 <filter> <filter-name>TestFilter</filter-name> ...

  4. 品味ZooKeeper之纵古观今_1

    品味ZooKeeper之纵古观今 本章思维导图 这一系列主要是从整体到细节来品味Zookeeper,先从宏观来展开,介绍zookeeper诞生的原因,接着介绍整体设计框架,接着是逐个细节击破. 本章是 ...

  5. ML理论知识博文列表

    一文弄懂神经网络中的反向传播法——BackPropagation:http://www.cnblogs.com/charlotte77/p/5629865.html 人脸识别图库:http://vis ...

  6. (转)Delphi7中QuickReport组件(QReport报表)安装方法及重要属性

    Delphi7中没有办法直接使用QuickReport组件,因为在Delphi7中没有将QuickReport组件包作为默认组件打包,如果要使用此组件,需要先安装一下.     打开delphi7,点 ...

  7. Qt 学习之路 2(53):自定义拖放数据

    Qt 学习之路 2(53):自定义拖放数据 豆子  2013年5月26日  Qt 学习之路 2  13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...

  8. Android 应用资源及R文件的位置

    1.介绍 (1)常识 (2)在res目录下新建资源文件(例如数字资源) app--->res,选择res,右击new--->value resource file 2.字符资源(strin ...

  9. P3233 [HNOI2014]世界树

    传送门 看到指定的总节点数小于等于 300000 就知道要搞虚树了 考虑如何在虚树确定每个议事处控制的节点数量 可以两遍dfs 第一遍求儿子对父亲的影响,第二遍求父亲对儿子影响 注意搜索顺序,这样就可 ...

  10. Java正则表达式-捕获组

    private Set<String> getCodes(String s) { Set<String> resultSet = new HashSet<>(); ...