【问题标题】:Api call from within WCF project to another WCF Project error从 WCF 项目中的 Api 调用到另一个 WCF 项目错误
【发布时间】:2018-02-08 05:14:09
【问题描述】:

我在理解为什么我得到“对预检请求的响应未通过访问控制检查:请求的资源上不存在'Access-Control-Allow-Origin'标头。来源”时遇到了一些大问题 所以为了更好地理解我的 webapi 项目有一个登录方法。该方法调用另一个 api(不同的服务器)进行登录以接收令牌。在客户端中,当我调用另一个项目时,我添加了标题(“Access-Control-Allow-Origin”,“”)。我还在 Azure 上启用了 cors。我什至在方法上方添加了 [EnableCors(origins:"",headers:"",methods:"",exposedHeaders:"*")]。

这是我的 webconfig 的一个高峰

<appSettings>
    <add key="LogFilePath" value="c:\SAB.Hosting.WebApi.Log.Xml" />
    <add key="LoginAddress" value="https://somewebsite/api/account/Login" />
    <add key="GetToken" value="https://somewebsite/api/account/GetToken" />
    <add key="RegisterAddress" value="https://somewebsite/api/account/Register" />
    <add key="LogoutAddress" value="https://somewebsite/api/account/Logout" />
    <add key="RegisterAddress" value="https://somewebsite/api/account/Register" />
    <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>

更新:我发现问题不是来自上面的链接。当我在我的 api 项目和我的 wcf 服务之间添加 SSL 证书时,就会发生这种情况。

    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="PersonService_HttpEndPoint" />
        <binding name="SABService_HttpEndPoint" maxReceivedMessageSize="2147483647">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="PersonService_TcpEndPoint">
          <security mode="None" />
        </binding>
        <binding name="SABService_TcpEndPoint">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="CertBehavior">
          <clientCredentials>
            <clientCertificate x509FindType="FindByThumbprint" findValue="93F67C44F8RB0DC5FD1CA8013F256B8F84C8E08G" storeLocation="CurrentUser" storeName="My" />
            <!--<clientCertificate findValue="PFX" x509FindType= "FindByExtension" />-->            
          </clientCredentials>
          <callbackDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </endpointBehaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <client>
      <endpoint address="https://someaddress/SABService.svc" behaviorConfiguration="CertBehavior" binding="basicHttpBinding" bindingConfiguration="SABService_HttpEndPoint" contract="SABServiceReference.ISABService" name="SABService_HttpEndPoint" />
      <endpoint address="https://someaddress/PersonService.svc" behaviorConfiguration="CertBehavior" binding="basicHttpBinding" bindingConfiguration="PersonService_HttpEndPoint" contract="PersonServiceReference.IPersonService" name="SABPersonService_HttpEndPoint" />
    </client>
  </system.serviceModel>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

我不知道为什么如果我添加 ssl 证书在登录时不会抛出 access-control-allow-origin。

等待您的解决方案。

谢谢你, 一个需要帮助的堕落程序员。

【问题讨论】:

    标签: c# wcf cors


    【解决方案1】:

    您应该使用 Microsoft.AspNet.WebApi.Cors nuget 在您的 WebApiConfig 中显式启用 CORS:

    public class WebApiConfig
    {
        // ...
    
        public void Register(HttpConfiguration config)
        {
            config.EnableCors(new EnableCorsAttribute("<origins>", "<headers>", "<methods>")
            {
                SupportsCredentials = true // If you need to pass them
            });
    
            // ...
        }
    }
    

    【讨论】:

    • 好吧,我没有使用 MVC,我使用的是 webconfig。这也可以在webconfig中添加?
    • @user2964720 AFAIK 使用 web.config 您只能为每个响应添加静态标头。看到这个问题 - stackoverflow.com/questions/29970793/…
    • @user2964720 顺便说一下,这个解决方案不仅适用于 MVC 项目。它也可以用于 OWIN 托管的任何 Web 应用程序。
    • 好的,非常感谢您的帮助,但请检查我现在添加的内容。我已经为我的问题添加了更新。
    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2011-12-08
    • 2016-09-25
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多