1

I have a Web API application that is authenticated using a combination of userid and an APIKey as part of the RequestMessage. In my authorization filter I check if the combination is valid before proceeding. I then need to use the userid to log some actions in my controllers.

The UserID and key are encoded together. Something like: .../api/v1/Transaction?api_key=YmFyZGFnYWR2ZXJndXI

The problem I am having is how to expose the userid to the controller.

6
  • 1
    What is the source of the userid? Commented May 4, 2016 at 11:26
  • It's part of the APIKey parameter in the request. The UserID and key are encoded together. .../api/v1/Transaction?api_key=YmFyZGFnYWR2ZXJndXI Commented May 4, 2016 at 11:36
  • @DavidPine Do you have an example for me to follow? Commented May 4, 2016 at 11:40
  • Can you show your filter? Commented May 4, 2016 at 11:41
  • 2
    About passing data from filter to controller: Implement a public property in your controller (UserId) (or one of its base types). In the filter, you can access the controller via the context. If it is of a compatible type, you can just set the property of the controller (e.g. after casting it). Commented May 4, 2016 at 11:41

1 Answer 1

1

Using the comment from Chips_100 I added CurrentUserId to my Controller and then this code to my AuthFilter:

public class CustomAuthFilter : AuthorizeAttribute
{
    private BaseController controller;
    public override void OnAuthorization(HttpActionContext actionContext)
    {   
        controller = actionContext.ControllerContext.Controller as BaseController;
        //Some code ommitted for clarity
        if (controller != null)
            controller.CurrentUserId = userAccount[0];
    }
}

Seems to work perfectly so far.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.