1

i need user request logging in action filter.

I had some refactoring to our companys web api project.It has custom authentication and authorization,i dont want to change this part.

Before the refactoring,every method find the requested user with header value.

[CustomAuthorization]
 public class ContentController : BaseApiController{

 public HttpResponseMessage Post(FromBody request){
   var token=GetFromHeader(request);
    var user=FindUser(token);
    Log.Write(user.Id,started,accessToken)
    Log.WriteDetail(requestContent,user.Id,methodName,accessToken)
    service.Action();
    Log.Write(user.Id,finished,accessToken)
  }
}

Every method has code block showing the above paragraph.Each controller inherited from BaseController,i added public property User and AccesToken etc. I set the these properties on OnAuthorization method in the CustomAuthorization.

 public override void OnAuthorization(HttpActionContext actionContext)
 {      
        var user=FindUserFromHeader(); 
        var controller = (BaseApiController)actionContext.ControllerContext.Controller;
        controller.User= user;
        controller.ServiceTicket = accessToken;

        actionContext.Request.Properties.Add(new KeyValuePair<string, object>("User", user));
        actionContext.Request.Properties.Add(new KeyValuePair<string, object>("AccessToken", accessToken));
 }

Now,i removed finding user blocks every method.

I want to remove logging in every methods.I added custom ActionFilterAttribute but i couldn't Get method request content.

In,my CustomLogAttribute:ActionFilterAttribute,i get Post data but i could'nt catch the GET method request content.How to achieve to get content data both of POST or GET method.

public override void OnActionExecuting(HttpActionContext actionContext)
        {
            object _customer;
            object _tranId;
            object _serviceTicket;
            actionContext.Request.Properties.TryGetValue("Customer", out _user);

            actionContext.Request.Properties.TryGetValue("AccessToken", out _serviceTicket);
            User= (User)_user;
            ServiceTicket = _serviceTicket.ToString();
            using (var stream = new MemoryStream())
            {
                var context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"];
                context.Request.InputStream.Seek(0, SeekOrigin.Begin);
                context.Request.InputStream.CopyTo(stream);
                string requestBody = Encoding.UTF8.GetString(stream.ToArray());
            }

            var content = actionContext.Request.Content.ReadAsStringAsync().Result;
        }
1
  • What do you mean by GET content? A GET request doesn't have a body, you would record the query params, etc. Apologies if I'm misunderstanding... Jan 29, 2018 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.