I am working on an authentication method using a token for me Web Api .Net project, so I am overriding some methods like this:
public class Authorizetest: System.Web.Http.AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if(Authorize(actionContext))
{
return;
}
HandleUnauthorizedRequest(actionContext);
}
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
base.HandleUnauthorizedRequest(actionContext);
}
private bool Authorize(HttpActionContext actionContext)
{
try
{
var context = new HttpContextWrapper(HttpContext.Current);
HttpRequestBase request = context.Request;
string token = request.Params["Token"];
return true;
}
catch (Exception)
{
return false;
}
}
}
I am using the decorator [Authorizetest] on this way:
[Authorizetest]
public class DoActionController : ApiController
{
[HttpPost]
public Display DoSomething(Parameter param)
{
//do something
return display;
}
}
But the request.Params is returning null however in the DoSomething method I get the value from Parameter.
I also have tried something like: (based on this page)
HttpRequestBase request = actionContext.RequestContext.HttpContext.Request;
string token = request.Params["Token"];
, but it's not possible to retrieve any value sent through the POST method.
I am using JQuery to send the data
$.ajax({
type: 'POST',
url: '/DoSomething',
data: JSON.stringify({ "Token": "xxxxxxxxx"}),
contentType: 'application/json; charset=utf-8',
success: function (data) {
},
fail:function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
How can I retrieve the data sent to DoSomething in the Authorizetest class?
datavariable.Params. This appears to be an XY problem. What is the ultimate goal you are trying to achieve?