2017-08-30 61 views
0

我在理解为什么得到“预检请求的响应没有通过访问控制检查:没有'访问控制允许源'头是出现在请求的资源。“Origin” 所以为了更好地理解我的webapi项目有一个登录方法。该方法调用另一个API(不同的服务器)进行登录以接收令牌。在客户端添加标题(“Access-Control-Allow-Origin”,“”),当我打电话给其他项目时。我还在Azure上启用了cors。我甚至在方法[EnableCors(源头:“”,标题:“”,方法:“”,exposedHeaders:“*”)]上添加了上述方法。从WCF项目到另一个WCF项目的API调用错误

这是我的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> 

更新的高峰:我发现这个问题是不是从联系above.This当我加入我的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。

等待您的解决方案。

谢谢, 有需要的堕落的程序员。

回答

0

你应该明确地启用CORS在您使用WebApiConfigMicrosoft.AspNet.WebApi.Cors的NuGet:

public class WebApiConfig 
{ 
    // ... 

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

     // ... 
    } 
} 
+0

好吧,我不使用MVC,我使用webconfig。这可以在webconfig中添加吗? – user2964720

+0

@ user2964720使用web.config AFAIK,您只能为每个响应添加静态标头。看到这个问题 - https://stackoverflow.com/questions/29970793/enabling-cors-through-web-config-vs-webapiconfig-and-controller-attributes –

+0

@ user2964720顺便说一句,这个解决方案不仅适用于MVC项目。它也可以用于OWIN托管的任何Web应用程序。 –