How to pass data from custom authorize attribute to web api action method?

Viewed 3209

I am creating a MVC Web API application with forms authentication for the Web API Controllers as well as the regular MVC controllers. In the form authentication cookie I am storing user information which I need to read and pass it to the Web API action methods. I am trying to do this by creating a custom Authorization attribute and adding this value to ActionArguments. However I don't know how to access this data in the Web API action. Below is the code block

public class MyAuthorization : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var isAuthorised = base.IsAuthorized(actionContext);

            if (isAuthorised)
            {
                var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                var identity = new GenericIdentity(ticket.Name);
                actionContext.ActionArguments["UserInfo"] = ticket.UserData;

            }
        }
    }

And here is the controller code

[RoutePrefix("api/test")]
    [MyAuthorization]
    public class TestWebAPIController : ApiController
    {
        [HttpGet]
        [Route("")]       
        public IHttpActionResult Get() {
            //How to get it here
            return Ok();
        }
    }
1 Answers
Related