我正在尝试在搜索功能中使用令牌。如何检查令牌是否正确?如果令牌正确,则检索正文以请求服务器。如果错误,则返回错误消息。
请求JSON
{
"header": {
"Token": "558fedce-a84e-4a9a-8698-5cd27d5af3ed"
},
"body": {
"WarehouseCode": "W001",
"CompanyCode": "C001"
}
}
例如,我的令牌是558fedce-a84e-4a9a-8698-5cd27d5af3ed,因此,如果正确,则获取正文部分并序列化以请求服务器。
public class TokenAuthorize: ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//gettting token from headers
IEnumerable<string> values;
if (!actionContext.Request.Headers.TryGetValues("Token", out values) || values.Count() > 1)
{
//if no token header found in rquest
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
else
{
//here is logic to validate your token
//if token is not proper than you can set response as unauthorized as above
}
}
}
然后在上面的方法上添加属性,如:
[TokenAuthorize]
public ActionResult Index(string track)
{
UploadToBCSSoftSCM b = new UploadToBCSSoftSCM();
string response = b.GetInvBal(track);
var data = JsonConvert.DeserializeObject<Rootobject>(response);
return View(data);
}