【问题标题】:CreatePerOwinContext causes 404 response in owin self hosted web-apiCreatePerOwinContext 在 owin 自托管 web-api 中导致 404 响应
【发布时间】:2015-01-01 23:38:34
【问题描述】:

我有一个自托管的 asp.net web api。我有几个工作正常的 api 控制器。所有请求都到达 api 方法,响应到达客户端。 现在我尝试为 web-api 添加授权。

我通过添加 AUTH SECTION 更改了我的启动配置。

   static void Main(string[] args)
        {
            StartOptions startOptions = new StartOptions();
            startOptions.Urls.Add("https://*:44305/");
            startOptions.Urls.Add("http://*:9001/");
            using (Microsoft.Owin.Hosting.WebApp.Start<dobStartUp>(startOptions))
            {
              //...
            }
       }
    public class dobStartUp
    {
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

        public static string PublicClientId { get; private set; }

        public void Configuration(IAppBuilder app)
        {
            //----AUTH SECTION---
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
           // PublicClientId = "self";
           // OAuthOptions = new OAuthAuthorizationServerOptions
           // {
           //     TokenEndpointPath = new PathString("/Token"),
           //     Provider = new ApplicationOAuthProvider(PublicClientId),
           //     AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
           //     AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
           //     AllowInsecureHttp = true
           // };
           // app.UseOAuthBearerTokens(OAuthOptions);
            //-------------

            var config = new HttpConfiguration();
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.Services.Replace(typeof(IAssembliesResolver),new dobAssembliesResolver());
            config.Formatters.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
           // config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Common.dobJsonContractResolver();
           var container = new UnityContainer();
            container.RegisterType<IdobRDBRepository, dobRDBRepository>(new TransientLifetimeManager());
            container.RegisterType<IResourcesContentRepository, AS3ContentRepository>(new TransientLifetimeManager());
            SystemDiagnosticsTraceWriter traceWriter = config.EnableSystemDiagnosticsTracing();
            traceWriter.IsVerbose = false;
            traceWriter.MinimumLevel = TraceLevel.Debug;
            config.DependencyResolver = new UnityDependencyResolver(container);
            config.MapHttpAttributeRoutes();
            app.UseWebApi(config);
        }
    }

当我在 auth-section 中使用 app.CreatePerOwinContext 时,会调用创建 appDbContext 和 appUserManager 的方法,但我对每个 api 方法都有 404 个响应。当我不调用这些 CreatePerOwinContext 方法时,每个方法都照常工作。我知道我可以使用我的 IoC 容器来制作 dbContext 和 userManager 的实例,但我想了解我当前与 app.CreatePerOwinContext 和 404 响应有关的问题。 提前谢谢大家。

【问题讨论】:

    标签: asp.net oauth-2.0 owin asp.net-web-api2 self-hosting


    【解决方案1】:

    我找到了 404 响应的原因。这是因为自定义程序集解析器。我在单独的 dll 中有控制器,这就是我使用自定义程序集解析器的原因,我在其中手动加载带有 api 控制器的程序集。在我只在 owin 管道中使用 web-api 之前,这一直很好。 接下来我尝试向 web api 添加授权 - 我使用了 [CreatePerOwinContext] 和 [app.UseOAuthBearerTokens] 所以我添加了中间件。它在 web-api 开始创建任何控制器之前加载引用的程序集。如果我在调试时理解正确,我已经两次加载了 api-controllers 程序集。因此在创建控制器实例时会导致冲突。

    【讨论】:

    • 我不确定您的解决方案是什么。你能粘贴你的新代码吗?谢谢!
    猜你喜欢
    • 2019-02-24
    • 2015-01-09
    • 2016-12-15
    • 2015-06-12
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多