在SharePoint 2010中进行身份验证,以自定义WCF服务自定义、身份验证、SharePoint、WCF

2023-09-09 21:40:41 作者:22.难深拥

我创建了一个自定义WCF服务的SharePoint 2010,而我试图通过一个jQuery Ajax请求来调用。我的自定义WCF服务是仿照这里所举的例子:

I've created a custom WCF service in SharePoint 2010 which I am trying to call via a JQuery Ajax request. My custom WCF service is modeled on the example given here:

链接:http://blog.sharepointbits.com/2010/04/custom-wcf-services-in-sharepoint-2010_17.html

上面的方法给了我一个WCF服务,我可以从C#服务器端code访问,和一个自定义InfoPath表单,但是,我无法得到使用JQuery的Ajax响应。

The above method gave me a WCF service I could access from C# server-side code, and a custom InfoPath form, however, I was unable to get a response using JQuery Ajax.

我对堆栈溢出其他地方阅读使用 MultipleBaseAddressWebServiceHostFactory 代替 MultipleBaseAddressBasicHttpBindingServiceHostFactory 为原创文章建议。

I read elsewhere on Stack Overflow to use MultipleBaseAddressWebServiceHostFactory instead of a MultipleBaseAddressBasicHttpBindingServiceHostFactory as the original article suggest.

链接:Sharepoint 2010 WCF服务。调用方法与jQuery

这工作,让我联系(但不进行身份验证)通过JQuery的Ajax的服务,但是,

This worked, allowing me to contact (but not authenticate to) the service via JQuery Ajax, however,

一)我不再能够浏览到http:// [服务器] /_vti_bin/Service.svc/mex,看到一个WSDL。这个问题意味着我的InfoPath表单无法连接到服务要么,因为他们寻找一个WSDL。

a) I am no longer able to navigate to http://[servername]/_vti_bin/Service.svc/mex and see a WSDL. This problem means my InfoPath forms cannot connect to the service either, because they look for a WSDL.

二)即使JQuery的Ajax请求点击自定义WCF服务,浏览器会询问我要认证,每一次,即使该请求来自一个用户登录到SharePoint的浏览器。

b) Even though the JQuery Ajax request hits the custom WCF service, the browser asks me for authentication every single time, even though the request comes from the browser of a user logged into SharePoint.

如果有人知道如何解决问题)和b)我会非常AP preciative。这真的不应该这样很难使可以从任何应用程序中使用的服务。

If anyone knows how to fix issues a) and b) I'd be very appreciative. It really shouldn't be so difficult to make a service that can be used from any application.

推荐答案

经过一番摆弄的时候,我坚持与使用 MultipleBaseAddressBasicHttpBindingServiceHostFactory 。而不是试图联系通过JSON的WCF服务,我做了一个函数来创建一个SOAP消息发送到WCF服务,然后分析结果。

After some mucking around, I stuck with the using MultipleBaseAddressBasicHttpBindingServiceHostFactory. Instead of trying to contact the WCF Service via JSON, I made a function to create a SOAP message, send that to the WCF service, and then parse the result.

有趣的是,这似乎已经解决了我的身份验证问题为好,虽然我不清楚为什么。

Interestingly, this seems to have solved my authentication issue as well, although I'm not clear as to why.

现在的服务是从的InfoPath和JavaScript的使用。

Now the service is usable from InfoPath and from JavaScript.

作为参考,从JavaScript的SOAP消息来WCF应该是这样的:

For reference, a SOAP message to WCF from JavaScript should look like:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">                
    <soap:Body>
       <MethodName xmlns="http://tempuri.org/"> 
           <ParamName1>Value</ParamName1>
           <ParamName2>Value</ParamName2>
       </MethodName> 
    </soap:Body> 
 </soap:Envelope>

和JQuery的发送它:

And the JQuery to send it:

$.ajax({
    type: "POST",
    url: url,
    data: soapEnvelope,
    timeout: timeOut,
    contentType: "text/xml",
    dataType: "xml",
    beforeSend: function (xhr) {
        xhr.setRequestHeader("SOAPAction", 'http://tempuri.org/' + methodPath);
    },
    success: onSuccess,
    error: onFailure
});

注:找出methodPath的值应该是看在WSDL为您服务的最简单方法

Note: The easiest way to find out what the value of methodPath should be is to look at the WSDL for your service.