gpt4 book ai didi

带有 Windows 身份验证的 WCF jsonP - 可能吗?

转载 作者:行者123 更新时间:2023-12-03 20:27:34 25 4
gpt4 key购买 nike

我正在托管一个输出 jsonp 的 wcf 服务。来自 IIS(打开 Windows 身份验证)的响应是

经过身份验证的服务不支持跨域 javascript 回调。

有没有办法解决这个问题?我必须打开 Windows 身份验证,但也想使用 wcf 来为我的 jsonp 提供服务

我的网络配置如下

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" >
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="ServiceSite.CustomersService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
</system.serviceModel>

最佳答案

有点晚了,我明白了,但由于没有发布答案,我遇到了类似的问题:

我能够使用从跨域客户端访问的经过 Windows 身份验证的 WCF 服务(托管在 IIs 7.5 中)的唯一方法是让客户端通过代理进行调用。有一个额外的跳回到代理,但现在我不再依赖 JSONP。该服务设置了两个端点,soap 和 json - 我将整个 serviceModel 贴在底部以供引用。

因此,客户端应用程序(都是 .NET Web 应用程序)要么:

A) 向页面方法(或 [HttpPost] MVC Controller 方法)发送 $.ajax POST,该方法将 WCF 作为soap web 引用调用:

function EmployeeSearch() {
var searchname = $("#userSearchText").val();
if (searchname.length > 0) {
var d = { name: searchname, pageSize: _pageSize, page: _currentPage };
var jsonData = JSON.stringify(d);
if (json.length > 0) {
$.ajax({
type: "POST",
url: "Home/EmployeeSearch",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: employeeSearchSuccess,
error: onError
});
}
}
}

和 Controller 方法(或页面方法):
    [HttpPost]
public ActionResult EmployeeSearch(string name, int pageSize, int page)
{
var client = new EmployeeServiceClient();
var searchResult = client.EmployeeSearch(name);
var count = searchResult.Count();
var employees = searchResult.Skip((page - 1) * pageSize).Take(pageSize).ToList();

var viewModel = new EmployeeSearchViewModel
{
Employees = employees,
Size = count
};

return Json(viewModel);
}

或者

B) 如 Dave Wards http://encosia.com/use-asp-nets-httphandler-to-bridge-the-cross-domain-gap/ 中所述,为 HttpHandler 制作一个 $.getJSON

在上面的示例中,处理程序的 ProcessRequest 方法中的 WebClient.DownoadString() 将采用以下 url 字符串:

http://server/EmployeeService/EmployeeService.svc/Json/EmployeeSearch/searchstring

这两种方法都允许我的服务保持在 Windows 身份验证下,并且客户端可以通过多种方式访问​​该服务。额外的啤酒花很烦人,但我尽量不去想它。

这是整个 WCF serviceModel 供引用:
<behaviors>
<serviceBehaviors>
<behavior name="EmployeeServiceBehavior">
<serviceTimeouts transactionTimeout="01:00:00"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>

<!-- we'd use this one if we wanted to ignore the 'd' wrapper around the json result ... we don't -->
<endpointBehaviors>
<!-- plain old XML -->
<behavior name="poxBehavior">
<webHttp helpEnabled="true" />
</behavior>
<!-- JSON -->
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>

</behaviors>

<bindings>
<basicHttpBinding>
<binding name="basicBinding"
hostNameComparisonMode="StrongWildcard"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="524288"
transferMode="Buffered"
messageEncoding="Text"
textEncoding="utf-8"
bypassProxyOnLocal="false"
useDefaultWebProxy="true" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<!-- use the following for windows authentication -->
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>

<webHttpBinding>
<binding name="webBinding"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
openTimeout="00:10:00"
closeTimeout="00:10:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="524288"
bypassProxyOnLocal="false"
useDefaultWebProxy="true"
>
<!--crossDomainScriptAccessEnabled="true"-->
<!-- use the following for windows authentication -->
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>

</bindings>

<services>
<service name="EmployeeService.Wcf.EmployeeService" behaviorConfiguration="EmployeeServiceBehavior">
<endpoint address="Soap" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="EmployeeService.Wcf.IEmployeeService"/>
<endpoint address="Json" binding="webHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="poxBehavior" contract="EmployeeService.Wcf.IEmployeeService" />
</service>
</services>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

添加一项 - 上述示例方法的 OperationContract 在 ServiceContract 中设置如下:
    [OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "EmployeeSearch/{name}")]
List<Employee> EmployeeSearch(string name);

关于带有 Windows 身份验证的 WCF jsonP - 可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7902938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com