我有一个WCF / SVC Web服务,通过
AJAX进行
JavaScript调用.
该页面通过位于DMZ中的SSL代理(https://gate.company.com/MyPage)访问,然后将请求转发到内部Web服务器(http:// myLocalWebServer / MyPage).
经过大量的googeling和尝试,我能够使它工作,使用参数crossDomainScriptAccessEnabled和Access-Control-Allow-Origin.虽然,它仅在身份验证模式设置为false或用户尚未登录时才有效.一旦从您需要登录的页面内进行调用(表单身份验证),它就不再起作用了.我得到的错误消息是:
cross domain javascript callback is not supported in authenticated services
但是,一旦我注销并从非受保护页面进行调用,它确实会再次起作用.
我的服务看起来像这样
namespace MyNameSpace { [ServiceContract(Namespace = "MyNameSpace")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service { [OperationContract] public string[] GetDropDownData(string id) { List<string> resultList = new List<string>(); ... return resultList.ToArray(); } } }
JavaScript中的服务调用和回调方法:
function fillDropdwon(dropId){ jQuery.ajax({ type: "POST",dataType: "jsonp",contentType: "application/json; charset=utf-8",cache: true,url: "Service.svc/GetDropDownData",data: '{"dropId":"' + dropId + '"}',jsonpCallback: "onDone",error: function (a,b,c) { alert("error"); } }); } // Callback-Methode after ServiceCall function onDone(result) { var theDropDown = jQuery("#<%= cboSelection.ClientID %>"); if (theDropDown.length > 0) { //Clear the old entries theDropDown.empty(); //Add an empty entry if ("<%= cboSelection.ShowEmptyRow %>".toLowerCase() == "true") { theDropDown.append($('<option></option>')); } // Add the found items for (var i = 0; i < result.length; i++) { var text = result[i]; theDropDown.append($('<option></option>').val(text).html(text)); } } }
涉及服务的web.config部分:
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="MyNameSpace.ServiceAspNetAjaxBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <standardEndpoints> <webScriptEndpoint> <standardEndpoint crossDomainScriptAccessEnabled="true" name=""/> </webScriptEndpoint> </standardEndpoints> <services> <service name="MyNameSpace.Service"> <!-- Service endpoint for HTTPS --> <endpoint address="" behaviorConfiguration="MyNameSpace.ServiceAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="jsonpBinding" contract="MyNameSpace.Service" /> --> </service> </services> <bindings> <webHttpBinding> <binding name="jsonpBinding" crossDomainScriptAccessEnabled="true"> <security mode="None" /> </binding> <binding name="jsonpSslBinding" crossDomainScriptAccessEnabled="true"> <security mode="Transport" /> </binding> </webHttpBinding> </bindings> </system.serviceModel>
我首先尝试使用ASP.NET AJAX代理来调用服务,但这不起作用,因为直接调用了web服务器,这不是SSL,我得到的错误或多或少:’Page https:/ /gate.company.com/MyPage尝试加载不保存来自页面http:// myLocalWebServer / MyPage …’的内容.这就是我使用上面列出的AJAX调用的原因.
function fillDropdwon(dropId){ var service = new MyNameSpace.Service(); service.GetDropDownData(dropId,onDone); }
我还尝试在web.config中添加以下内容
<system.webServer> <httpProtocol> <customHeaders> <!-- Enable Cross Domain AJAX calls --> <remove name="Access-Control-Allow-Origin" /> <add name="Access-Control-Allow-Origin" value="https://gate.company.com"/> </customHeaders> </httpProtocol> </system.webServer>
我检查了发送到服务器的标头,看到当我没有登录时,标题看起来像这样:
Request URL:`https://gate.company.com/MyPage/Servic.svc/GetDropDownData?callback=onDone` Request Method:POST Status Code:200 OK Request Headersview source Accept:text/javascript,application/javascript,application/ecmascript,application/x-ecmascript,*/*; q=0.01 Accept-Encoding:gzip,deflate,sdch Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,fr-CH;q=0.2,fr;q=0.2 Connection:keep-alive Content-Length:161 Content-Type:application/json; charset=UTF-8 Cookie:__utma=174172730.1157990369.1360852643.1381229705.1383150435.9; __utmc=174172730; __utmz=174172730.1369635484.4.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); promopost=oaezz3fzzj0o4l3fccxh0ss1; ASP.NET_SessionID= Host:`gate.company.com` Origin:`https://gate.company.com` Referer:`https://gate.company.com/MyPage/QuickCalculator.aspx?ObjectIdentity=47a93f52-6be6-4bd6-9600-e8eb9c8ff360` User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/31.0.1650.48 Safari/537.36 X-Requested-With:XMLHttpRequest Query String Parametersview sourceview URL encoded callback:onDone Request Payloadview source {dropId:123} dropId: "123" Response Headersview source Cache-Control:private Connection:Keep-Alive Content-Encoding:gzip Content-Length:1339 Content-Type:application/x-javascript Date:Sun,01 Dec 2013 15:14:25 GMT Keep-Alive:timeout=15,max=97 Server:Microsoft-IIS/7.5 Vary:Accept-Encoding X-AspNet-Version:4.0.30319 X-Powered-By
并且响应看起来像这样.
onDone(["result1","result2"]);
当我从受保护的页面中调用服务时,我得到了这个:
Request URL:`https://gate.company.com/MyPage/Servic.svc/GetDropDownData?callback=onDone` Request Method:POST Status Code:200 OK Request Headersview source Accept:text/javascript,fr;q=0.2 Connection:keep-alive Content-Length:161 Content-Type:application/json; charset=UTF-8 Cookie:__utma=174172730.1157990369.1360852643.1381229705.1383150435.9; __utmc=174172730; __utmz=174172730.1369635484.4.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); promopost=oaezz3fzzj0o4l3fccxh0ss1; **ASP.NET_SessionID=; .ASPXAUTH=AB5ADCE12C7847CA452DD54D903E6787C7D1F0009B9E3277D2EC50DE9C421D1331B87A6DCA2432993933794AB9BDE833E44EC58E217D5AA1D588132C6E1C67D4AD7692840359D9A719EC2A53826CF54FDC0943B4E0AB29093920143E1E987080AC7C35E63594FD678535972D06AEC0AAF74AF8BE8DFC3746B499CB032E7771F10B924110DB344824B3253F9BECB3CDD8** Host:`gate.company.com` Origin:`https://gate.company.com` Referer:`https://gate.company.com/MyPage/QuickCalculator.aspx?ObjectIdentity=47a93f52-6be6-4bd6-9600-e8eb9c8ff360` User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,01 Dec 2013 15:14:25 GMT **jsonerror:true** Keep-Alive:timeout=15,max=97 Server:Microsoft-IIS/7.5 Vary:Accept-Encoding X-AspNet-Version:4.0.30319 **X-Powered-By:ASP.NET**
并且响应看起来像这样.
onDone({"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"Cross domain javascript callback is not supported in authenticated services.","StackTrace":" bei System.ServiceModel.Dispatcher.JavascriptCallbackMessageInspector.AfterReceiveRequest(Message& request,IClientChannel channel,InstanceContext instanceContext)\u000d\u000a bei System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.AfterReceiveRequestCore(MessageRpc& rpc)\u000d\u000a bei System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\u000d\u000a bei System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)","Type":"System.NotSupportedException"},"ExceptionType":"System.NotSupportedException",InstanceContext instanceContext)\u000d\u000a bei System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.AfterReceiveRequestCore(MessageRpc& rpc)\u000d\u000a bei System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\u000d\u000a bei System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"},500);
主要区别在于,对于“登录”版本,存在SessionID和jsonerror:true.
有办法解决这个问题吗?
是否无法通过在调用之前更改标头或“类似”来“禁用”AJAX请求的身份验证.或者我的代码web.config中是否有任何错误?
我很感激任何暗示,因为我在努力了很长时间.
解决方法
我终于找到了解决问题的方法.我写下了我为解决方案所采取的步骤,并希望这将有助于解决类似问题的任何人.
我首先使用ASP.NET AJAX代理并进行了这样的调用.
var service = new SDAG.Post.PPT.Website.Service(); service.GetDropDownData(dropId,onDone);