Having recently started using OWIN to handle authorization, I’ve had to change a few things throughout my web api projects. To have the user’s identity available, I usually create something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
This will add the ClaimsPrinciple using composition allowing me to use Ninject like this:
1 2 3 | |
Now I can get to the logged in user’s Id and claims/roles by using constructor injection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
It also allows me to get to the principal when I am no longer in the web api project. My repository/service/flavour of the month class simply has a contructor with the IMyPrincipal argument.
Previously, using FormsAuth, the principal was available as an IPrincipal using HttpContext.Current.User meaning we could use inheritance. The OWIN approach leads us down the (currently) preferred composition route :–)