我有带有工作认证等的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