WebApi - 注入oauth登录用户的ID

时间:2018-06-07 14:10:09

标签: c# asp.net-web-api oauth

我有带有工作认证等的Web API应用程序。现在我希望能够(N)注入一些关于当前登录用户的数据。所以我已经将ninject配置为看起来像这样:

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

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

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

但是当调用此方法时,LogonUserIdentity看起来与通过OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials创建的身份不同,但更像是本地Windows帐户身份。没有ID或电子邮件(名称是我的本地帐户名称)。另一方面,HttpContext.Current.User是空的&#34; (没有名字,没有身份证。)

如何检索当前的OAuth用户?

解决

在此处找到解决方案:User (IPrincipal) not avaliable on ApiController's constructor using Web Api 2.1 and Owin

0 个答案:

没有答案