WebAPI Token Verification
Login Port
HttpContext.Current.Session.Timeout = 10;
Generate ticket
FormsAuthenticationTicket token = new FormsAuthenticationTicket (0,req. Lusername,datetime.now,datetime.now.addminutes (Ten), true,string. Format ("{0}& {1}", req. Lusername,req. Lupassword), Formsauthentication.formscookiepath);
var token = Formsauthentication.encrypt (token);
httpcontext.current.session["UserName"] = req. Lusername;
Logininfo.ticket = Token;
Filter filters
var authorization = actionContext.Request.Headers.Authorization; if (authorization! = NULL) && (authorization. Parameter = null)) {var encryptticket = authorization. Parameter; if (Validateticket (encryptticket)!=null) {userName = Validateticket (Encryptticket);}}
Decryption method
private string Validateticket (string encryptticket) {var strticket = Formsauthentication.decrypt (encryptticket). UserData; Get username and password from ticket var index = strticket.indexof ("&"); String struser = strticket.substring (0, index); String strpwd = strticket.substring (index + 1); httpcontext.current.session["UserName"] = struser; httpcontext.current.session["Token"] = strpwd; return struser; }
Ajax requests
function Bookinglist () {var token= ' @ViewBag. token '; $.ajax ({//type of submitted data post GET type: "POST",//URL of submitted URL: "Http://local Host:12426/api/blocking/blockinglist ",//Data submitted: {" SearchText ":" "}, Beforesend:function (XHR) {Xhr.setrequesthea Der (' Authorization ', ' BasicAuth ' +token); }, Async:false,//false is synchronous, true to the format of the asynchronous//Return Data datatype: "HTML",//"XML", "HTML", "script", "JSON", "Jsonp", "text". The function success:function (result) {for (var i = 0; I < result) is called after successful return. Totalblockingcount; i++) {//alert (Result.blockdatalist[i]. MemberName); $ ("#txtContent"). attr ("value", Result.blockdatalist[i]. MemberName); }},//Call function Error:function (XMLHttpRequest, Textstatus, Errorthrown) {//#3这个error函数调试时非常有用, if parsing is incorrect, an error box will pop up alert ( Xmlhttprequest.status); alert (xmlhttprequest.readystate); alert (textstatus); Paser error; } }); }
WebAPI Token Verification