c# - WCF 自定义 JSONP 绑定(bind)和 httpsTransport

标签 c# wcf rest iis-6

我的问题围绕着用 JSONP 响应的 IIS 的 WCF REST 服务。我参加了这个解决方案中的类(class):http://msdn.microsoft.com/en-us/library/cc716898.aspx并将它们添加到我的。使用 httpTransport 模拟在我的开发人员工作站上一切正常,但是当我尝试移动到开发服务器时,我遇到了一些安全问题。使用下面的配置和应用程序池身份用户解决了这些问题。我还为仅 NTLM 身份验证配置了 IIS 元数据库文件(我们正在使用 IIS 6,但很快就会成为 IIS 7,需要在两者上工作)因为我无权创建 SPN。我相信当前配置解决了我的安全问题但在此过程中我的 JSONP 响应被降级为常规 JSON,这就是问题所在。下面是相关配置:

    <services>
        <service name="IS.Core.Infrastructure.RESTRouter.Transactions" behaviorConfiguration="">
            <endpoint address="" behaviorConfiguration="webHttp" binding="customBinding"
              bindingConfiguration="jsonpBinding" contract="IS.Core.Infrastructure.RESTRouter.ITransactions">
            </endpoint>
        </service>

        <service name="IS.Core.Infrastructure.RESTRouter.Queue" behaviorConfiguration="">
            <endpoint address="" behaviorConfiguration="webHttp"  binding="customBinding"
                bindingConfiguration="jsonpBinding" contract="IS.Core.Infrastructure.RESTRouter.IQueue" />
        </service>
    </services>

    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttp">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>

    <bindings>
        <customBinding>
            <binding name="jsonpBinding">
                <jsonpMessageEncoding />
                <httpsTransport
                      manualAddressing="true"
                      authenticationScheme="Ntlm" />
            </binding>
        </customBinding>
    </bindings>

    <extensions>
        <bindingElementExtensions>
            <add name="jsonpMessageEncoding"
              type="IS.Core.Infrastructure.RESTRouter.JsonpBindingExtension, RESTRouter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </bindingElementExtensions>
    </extensions>

这是接口(interface)方法定义之一:

    [OperationContract]
    [WebGet(UriTemplate = "{ModelPath}/{ObjectTypeName}?callback={callback}", ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    JSONPXml NewObject(string ModelPath, string ObjectTypeName, string callback);

这是它的实现:

    [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
    public JSONPXml NewObject(string ModelPath, string ObjectTypeName, string callback) {

        int val = getEmployeeIdByNTUsername(OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name);

        JSONPXml jsp = null;
        EntityPluginReflectorClient client = null;
        try {
            client = new EntityPluginReflectorClient();
            string output = client.NewObject(ModelPath, ObjectTypeName);
            jsp = new JSONPXml() { xml = output };
        } catch (Exception e) {
            InfrastructureLog.WriteException(this, "NewObject", e);
            jsp = getExceptionResponse(e);
        }
        finally {
            client.Close();
        }
        return (jsp);
    }

这是数据契约(Contract):

[DataContract()]
public class JSONPXml {
    public JSONPXml() { }
    [DataMember]
    public string xml;
}

如果需要更多信息,请告诉我,感谢您对此的调查。

最佳答案

我不是 100% 确定答案,但这里有几件事可以帮助您缩小范围:

对于初学者来说,如果您设置 ProtectionLevel显式地指定给 Sign 或 EncryptAndSign,那么您必须使用启用了安全性的绑定(bind),否则将抛出异常。如果您尝试通过 http 访问它,它将开始抛出异常,这可以帮助您弄清楚您实际是如何访问该服务的。

其次,由于您使用的是 customBinding,因此您需要告诉它您希望绑定(bind)中的安全类型。我认为仅指定 httpsTransport 是不够的。你这样做的方式是通过 security tag .从它的声音来看,您需要设置 authenticationMode="SspiNegotiated"

根据 the custom binding docs

The order in which elements appear in the stack matters, because it is the order in which operations are applied to the message. The recommended order of stack elements is the following:

Transactions (optional)

Reliable Messaging (optional)

Security (optional)

Transport

Encoder (optional)

有关自定义绑定(bind)安全性的更多信息 herehere 希望对您有所帮助。

关于c# - WCF 自定义 JSONP 绑定(bind)和 httpsTransport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1440269/

相关文章:

c# - "Authentication failed because the remote party has closed the transport stream"普通用户

c# - RESTful 服务基本身份验证

c# - 异步操作中的异步操作

c# - 我应该在使用 WCF 的应用程序中将比较器放在哪里?

c# - 如何在 WCF 中为客户端配置 net.tcp 绑定(bind)

c# - 命名 Rest URL

java - 更换 Web 服务的最佳实践?

c# - LINQ to Entities 日期时间比较

c# - 数组中值的随机索引

c# - 从 SQL Server 2008 数据库中选择列中的所有值?