【问题标题】:RequestContext Principal always nullRequestContext 主体始终为空
【发布时间】:2017-04-14 00:48:47
【问题描述】:

我创建了一个令牌 .NET Web API 端点来获取 OAuth 持有者令牌。检索令牌后,我调用我的 API 的另一个端点,并在授权标头中传递我的承载令牌,但如果我检查 RequestContext 中的主体,它始终为空。为什么我的 owin 服务器没有创建主体对象。

这样注册我的授权提供者是否正确:

 builder.RegisterType<SimpleAuthorizationServerProvider>()
            .As<IOAuthAuthorizationServerProvider>()
            .PropertiesAutowired()
            .SingleInstance(); 

或者应该是 .InstancePerRequest()。但是,如果我按请求使用实例,则会收到错误消息。有没有人有这个用例的工作示例。 我非常绝望,因为我尝试了多个示例但没有解决方案。这甚至是我关于这个问题的第二个线程 (Token Based Authentication using ASP.NET Web API 2 and Owin throws 401 unautorized),但我认为我的问题在于 autofac,因为它是与其他工作示例的唯一区别。

【问题讨论】:

    标签: oauth asp.net-web-api2 owin autofac


    【解决方案1】:

    这个答案解决了我的问题 https://stackoverflow.com/a/36769653/5441093

    更改 GrantResourceOwnerCredentials 方法以解析我的 userbl 类:

    var autofacLifetimeScope = OwinContextExtensions.GetAutofacLifetimeScope(context.OwinContext);
            var userBl = autofacLifetimeScope.Resolve<IUserBl>();
    

    而不是使用autofac的注入

    【讨论】:

      猜你喜欢
      • 2012-03-18
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2017-07-31
      • 2019-07-18
      • 2019-08-18
      相关资源
      最近更新 更多