WebAPI Token 验证

2021-04-11 16:25

阅读:620

标签:user   string   erro   timeout   tick   api   sub   jsonp   block   

WebAPI Token 验证

登录端

        //HttpContext.Current.Session.Timeout = 10;

        ////生成Ticket

        //FormsAuthenticationTicket token = new FormsAuthenticationTicket(0,req.LUsername,DateTime.Now,DateTime.Now.AddMinutes(10),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;

 

过滤器

var authorization = actionContext.Request.Headers.Authorization; if ((authorization != null) && (authorization.Parameter != null)) { var encryptTicket = authorization.Parameter; if (ValidateTicket(encryptTicket)!=null) { userName = ValidateTicket(encryptTicket); } }

解密方法

private string ValidateTicket(string encryptTicket) { var strTicket = FormsAuthentication.Decrypt(encryptTicket).UserData; //从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请求

function bookingList() { var token=‘@ViewBag.Token‘; $.ajax({ //提交数据的类型 POST GET type: "POST", //提交的网 址 url: "http://localhost:12426/api/Blocking/BlockingList", //提交的数据 data: { "SearchText": "" } , beforeSend : function(XHR){ XHR.setRequestHeader(‘Authorization‘, ‘BasicAuth ‘ +token); } , async: false,//false为同步,true为异步 //返回数据的 格式 datatype: "html",//"xml", "html", "script", "json", "jsonp", "text". //成功返回之后调用的函数 success: function (result) { for (var i = 0; i

WebAPI Token 验证

标签:user   string   erro   timeout   tick   api   sub   jsonp   block   

原文地址:https://www.cnblogs.com/eimers/p/9025608.html


评论


亲,登录后才可以留言!