2017-08-30 63 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應用程序。 –