Skip to main content
 首页 » 编程设计

c#之Web API 2 OWIN Self Host 只能通过 IP 地址连接

2024年05月29日72findumars

我使用 Microsoft OWIN 3.0.1 和 Web API 5.2.3 NuGet 包创建了一个自托管服务。它还使用 Microsoft.AspNet.WebApi Cors.5.2.3 和 Microsoft.Owin.Cors 3.0.1 包来支持 CORS。 CORS 设置配置如下:

private static void ConfigureCrossOriginResourceSharing(IAppBuilder app, HttpConfiguration config) 
{ 
    app.UseCors(CorsOptions.AllowAll); 
 
    var cors = new EnableCorsAttribute("*", "*", "*"); 
    config.EnableCors(cors); 
} 

托管启动如下:

var startOptions = new StartOptions(url: "http://*:64000/"); 
 
Task.Run(() => 
{ 
    // Start OWIN host  
    using (WebApp.Start(startOptions, startUp.Configuration)) 
    { 
        //etc. 

当连接到该服务时,我只能使用 IP 地址来调用它(例如 http://192.168.0.1:64000/)。使用 DNS 名称(即 http://hostname:64000/ )时,会返回 HTTP 502 Bad Gateway 错误。这并不理想。

我认为这与 CORS 配置有关。 有什么问题吗?

更新:根据下面的建议,我已将开始 url 更改为 http://+:64000/ .我现在收到来自客户端的 400 Bad Request 响应。我可以 ping 解析为完全限定名称(例如 hostname.domain.corp 域后缀)的服务器,我也尝试过但得到相同的结果。

请您参考如下方法:

如果您想使用 Kerberos 进行身份验证,您需要使用 AD 为您用于应用程序的主机名/端口组合注册一个 SPN。