WebAPI Security Using token+ signature Verification (bottom)

Source: Internet
Author: User
Tags httpcontext tojson

Web Tutorial: WebAPI Security Using token+ signature Verification (bottom)

Stitching parameters based on request type

NameValueCollection form = HttpContext.Current.Request.QueryString;

String data = String. Empty;

Switch (method)

{

Case "POST":

Stream stream = HttpContext.Current.Request.InputStream;

String Responsejson = String. Empty;

StreamReader StreamReader = new StreamReader (stream);

data = Streamreader.readtoend ();

Break

Case "GET":

First step: Remove all get parameters

IDictionary parameters = new Dictionary ();

for (int f = 0; f < form. Count; f++)

{

String key = form. KEYS[F];

Parameters. ADD (Key, Form[key]);

}

Step two: Sort the dictionary in alphabetical order by key

IDictionary sortedparams = new SortedDictionary (parameters);

Ienumerator> dem = Sortedparams.getenumerator ();

Step three: String all parameter names and values together

StringBuilder query = new StringBuilder ();

while (Dem. MoveNext ())

{

String key = Dem. Current.key;

String value = Dem. Current.value;

if (!string. IsNullOrEmpty (Key))

{

Query. Append (Key). Append (value);

}

}

data = query. ToString ();

Break

Default

resultmsg = new Resultmsg ();

Resultmsg.statuscode = (int) statuscodeenum.httpmehtoderror;

Resultmsg.info = StatusCodeEnum.HttpMehtodError.GetEnumText ();

Resultmsg.data = "";

Actioncontext.response = Httpresponseextension.tojson (Jsonconvert.serializeobject (RESULTMSG));

Base. OnActionExecuting (Actioncontext);

Return

}

BOOL result = Signextension.validate (timestamp, nonce, ID, signtoken,data, signature);

if (!result)

{

resultmsg = new Resultmsg ();

Resultmsg.statuscode = (int) statuscodeenum.httprequesterror;

Resultmsg.info = StatusCodeEnum.HttpRequestError.GetEnumText ();

Resultmsg.data = "";

Actioncontext.response = Httpresponseextension.tojson (Jsonconvert.serializeobject (RESULTMSG));

Base. OnActionExecuting (Actioncontext);

Return

}

Else

{

Base. OnActionExecuting (Actioncontext);

}

}

public override void OnActionExecuted (Httpactionexecutedcontext actionexecutedcontext)

{

Base. OnActionExecuted (ActionExecutedContext);

}

}

Then we test to verify the legality of the API request.

GET Request:

1. Get product data, pass parameter Id=1,name= "Wahaha", complete request is Http://localhost:14826/api/product/getproduct?id=1&name=wahaha

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-161024095531a5.png "alt=" Chengdu Dahne "style=" border:0px; "/>

2. Request Header Add Timespan,staffid,nonce,signature field

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-161024095552250.png "alt=" Chengdu Dahne "style=" border:0px; "/>

3. When the value in data is Id1namewahaha, the value of the signature in the request header and the result computed by the server side is exactly the same, and when I modify data to ID1NAMEWAHAHA1, The server-side computed signature result and the signature that are submitted in the request header are not the same, and are represented as illegal requests.

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-161024095601562.png "alt=" Chengdu Dahne "style=" border:0px; "/>

4. Illegal requests will be identified as request parameters have been modified

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-16102409560S95.png "alt=" Chengdu Dahne "style=" border:0px; "/>

POST request:

1.post object is serialized into a JSON string and submitted to the background, back to the corresponding product information

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-16102409561GC.png "alt=" Chengdu Dahne "style=" border:0px; "/>

2. Background get parameter information of request

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-161024095624Q8.png "alt=" Chengdu Dahne "style=" border:0px; "/>

3. Determine if the signature is successful, the first request signature parameter signature and the server side of the calculation result is exactly the same, and then when the number of count in the request parameter from 10 to 100 after the server-side calculation of the result and request signature parameters signature different, So the request is illegal, the request is unlawful, similarly, if any other parameter is modified, the result of the calculation will be different from the signature parameter, the request is also recognized as an illegitimate request

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161024/1-161024095632618.png "alt=" Chengdu Dahne "style=" border:0px; "/>

Summarize:

Through the above case, we can see that the key to security is to participate in the signing token, the whole process of token is not involved in communication, so long as the token is not disclosed, the request will not be forged.

We then use the timestamp timestamp to verify that the request is out of date, which is not valid even if the full request link is taken.

Sign signature way to prevent information tampering and forgery to a certain extent, to ensure the security of communications


WebAPI Security Using token+ signature Verification (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.