0

I have Web API application with working authentication etc. Now I want to be able to (N)inject some data about currently logged user. So I've configured ninject to look something like this:

kernel.Bind<UserContext>().ToMethod(ctx => 
{
    var identity = HttpContext.Current?.Request?.LogonUserIdentity

    if (identity != null)
    {
        return new UserContext
        {
            UserId = identity.GetUserId(),
        };
    }

    return new UserContext();
}).InRequestScope()

But when this method gets to be called the LogonUserIdentity looks nothing like identity created via OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials but more like local windows account identity. There is no id or email (name is my local account name). HttpContext.Current.User on the other hand is "empty" (no name, no id).

How can I retrieve current OAuth user?

Solved

Found solution here: User (IPrincipal) not avaliable on ApiController's constructor using Web Api 2.1 and Owin

Krzysztof
  • 498
  • 5
  • 23
  • Ok, from what i''ve established it looks like the proper user identity is set somewhere between controller creation and action invoke. When referenced in controller's constructor User.Idenity is still empty but is set during action execution. Can't find any documentation about this though... – Krzysztof Jun 08 '18 at 08:03
  • I found the reason and solution here: https://stackoverflow.com/questions/23785528/user-iprincipal-not-avaliable-on-apicontrollers-constructor-using-web-api-2-1 – Krzysztof Jun 08 '18 at 09:31

0 Answers0