使用WSFederationHttpBinding的性能非常糟糕

时间:2012-06-27 07:46:32

标签: c# wcf wif ws-federation

使用WSFederationHttpBinding我的性能非常差 - 每秒只处理250个请求。

结合:

public class CustomFactoryActive : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            ServiceHost host = new ServiceHost(serviceType, baseAddresses);
            CommonConf.ConfigureServiceHost(host);


            string issuerAddress = ConfigManager.ActiveSTS;
            string issuerMexAddress = issuerAddress + "/mex";

            WSFederationHttpBinding wsFedBinding = new WSFederationHttpBinding();
            wsFedBinding.Security.Mode = WSFederationHttpSecurityMode.Message;
            wsFedBinding.ReliableSession.Enabled = false;

            wsFedBinding.MaxReceivedMessageSize = wsFedBinding.MaxBufferPoolSize = Constants.MaxFileSize;

            XmlDictionaryReaderQuotas quotas = wsFedBinding.ReaderQuotas;
            quotas.MaxArrayLength = quotas.MaxBytesPerRead = quotas.MaxStringContentLength =
                quotas.MaxNameTableCharCount = quotas.MaxDepth = (int)Constants.MaxFileSize;

            var messageSecurity = wsFedBinding.Security.Message;

            messageSecurity.IssuedTokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1";
            messageSecurity.IssuedKeyType = SecurityKeyType.SymmetricKey;
            messageSecurity.EstablishSecurityContext = false;
            messageSecurity.NegotiateServiceCredential = false;

            messageSecurity.IssuerAddress = new EndpointAddress(new Uri(issuerAddress));
            messageSecurity.IssuerMetadataAddress = new EndpointAddress(new Uri(issuerMexAddress));


            WS2007HttpBinding ws2007HttpBinding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);
            var wsHttpSecurity = ws2007HttpBinding.Security;
            wsHttpSecurity.Message.ClientCredentialType = MessageCredentialType.UserName;//авторизация по логину и паролю
            wsHttpSecurity.Message.NegotiateServiceCredential = true;
            wsHttpSecurity.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

            messageSecurity.IssuerBinding = ws2007HttpBinding;

            ContractDescription contractDescription = ContractDescription.GetContract(typeof(ISignService));

            EndpointAddress endpointAddress = new EndpointAddress(baseAddresses[0]);
            ServiceEndpoint endpoint = new ServiceEndpoint(contractDescription, wsFedBinding, endpointAddress);
            host.Description.Endpoints.Add(endpoint);

            return host;
        }
    }

我的wcf测试方法什么都不做 - 它只返回1个字节。

但是当我使用带有消息安全性的简单WSHttpBinding而没有任何WIF saml令牌时,我得到了大约。每秒4000个请求

我无法理解为什么

5 个答案:

答案 0 :(得分:6)

您应该设置和配置WCF跟踪以查看WCF架构中花费时间的大纲,有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms733025.aspx

启用跟踪并查看请求时,您可能看到(在单个呼叫者多次呼叫相同服务的测试环境中)STS仅被呼叫一次,后续调用包含缓存的令牌。但是,所有呼叫仍将设置安全连接,从而为每次呼叫验证令牌(这将花费一些CPU时间)。或者,您可以/可以通过方法级别分析服务主机来验证所有这些,这将更清楚地显示完全花费时间的位置。

答案 1 :(得分:3)

模拟安全令牌(不要使用实际的STS)并查看性能如何。我认为那部分是你的瓶颈。

答案 2 :(得分:3)

我看到了NegotiateServiceCredential = true在打开频道时添加了多次网络往返的情况。尝试将wsHttpSecurity.Message.NegotiateServiceCredential更改为false,并在客户端指定服务类别。

答案 3 :(得分:3)

证书验证也可能需要花费大量时间。您可以尝试更改客户端的行为,以便跳过证书验证和撤销。

//Client
var clientCredentialsBehavoir = behaviors.Find<FederatedClientCredentials>();
clientCredentialsBehavoir.ServiceCertificate.Authentication.CertificateValidationMode =                     X509CertificateValidationMode.None;
clientCredentialsBehavoir.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;

//The same can be done on the server
var serviceConfiguration = new ServiceConfiguration();
serviceConfiguration.CertificateValidationMode =   X509CertificateValidationMode.None;

这不应该在生产环境中完成!

您也可以通过appconfig执行此操作。

答案 4 :(得分:2)

SAML令牌已加密 - 具体取决于您的配置。在您的情况下,您似乎正在使用SecurityAlgorithmSuite.Default,我认为这是AES256

每当发生解密/加密时,CPU都需要花费一些时间来进行数字运算。所花费的时间/精力取决于多种因素,但我不认为您看到所见的请求处理功能的差异是 不正常。

正如其他响应所提到的,即使令牌被缓存,令牌仍然必须经过验证和解密,每个令牌都涉及通过一个或多个算法运行令牌的内容。

我的建议:使用不同的SecurityAlgorithmSuite常量执行一些分析,并比较结果。

一个地方可以将SecurityAlgorithmSuite.Basic128.Basic256进行比较。 256常量等同于默认选项。