塑胶托盘东莞网站建设,网站被抄袭怎么投诉,网站开发公司 上海,wordpress图片延迟加载在这个分为三部分的系列中#xff0c;我将演示如何使用SoapUI API工具来调用安全的WCF SOAP服务。 第一篇文章将着重于创建将要测试的系统的服务。 第二篇文章将介绍在基本身份验证机制保护的情况下调用它所需的步骤。 在最后一部分中#xff0c;我将对初始服务稍作更改… 在这个分为三部分的系列中我将演示如何使用SoapUI API工具来调用安全的WCF SOAP服务。 第一篇文章将着重于创建将要测试的系统的服务。 第二篇文章将介绍在基本身份验证机制保护的情况下调用它所需的步骤。 在最后一部分中我将对初始服务稍作更改以使其受证书认证机制的保护。 在同一篇文章中我还将演示如何使用SoapUI调用它。 WCF基本身份验证服务 使用“ 基本身份验证”传输安全性机制来保护对本文中要实现的服务中的资源的访问。 Windows Communication Foundation 提供的众多功能之一。 这种机制与HTTPS结合使用以提供机密性。 这项服务公开了一个端点该端点计算出很大的一笔款项 。 为了快速入门我们将使用Visual Studio 2019中提供的WCF服务应用程序的默认模板。 在菜单文件中依次单击新建项目或单击开始页面开始一个新项目。 让我们将解决方案和项目命名为AVeryBigSum_BasicAuthentication 。 现在您将看到已经添加到WCF服务项目中的几个文件。 我们可以选择删除接口IService1.cs和服务Service1.svc文件来创建新文件。 否则我们可以重命名这两个文件因此请注意重命名以及Service.svc文件的标记方法是右键单击它-“ 查看标记”并更改为以下名称。 % ServiceHost LanguageC# Debugtrue
ServiceAVeryBigSum_BasicAuthentication.Avbs CodeBehindAvbs.svc.cs % 重命名两个文件后打开IAvbs.cs复制以下代码并将其添加到修改后的界面中。 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;namespace AVeryBigSum_BasicAuthentication
{[ServiceContract]public interface IAvbs{[OperationContract]long AVeryBS(long[] ar);}
} 如果您选择删除这些文件则可以通过右键单击项目并添加新项来添加新界面。 选择接口模板并将其重命名为IAvbs。 同样您需要复制上面的代码并将其添加到新创建的界面中。 该服务仅实现接口协定中定义的一项操作。 要实现它我们需要修改VStudio创建的默认文件或添加一个新服务类Avbs.svc它将实现上面定义的接口。 using System;
/*...*/
namespace AVeryBigSum_BasicAuthentication
{public class Avbs : IAvbs{public long AVeryBS(long[] ar){long aVeryBigSum 0;foreach (long i in ar) aVeryBigSum i;return aVeryBigSum;}}
} 到目前为止我们已经定义了服务合同即带有示例定义的操作。 现在我们必须定义其端点。 要添加端点我们需要更改配置文件web.config。 除了复制和粘贴外我们还需要了解每个WCF标签的重要性。 使用SoapUI 1-Secure WCF SOAP – AppSettings 因此让我们从AppSettings元素开始。 此元素包含自定义应用程序设置。 该元素存储定制应用程序配置信息例如数据库连接字符串文件路径XML Web服务URL或应用程序的任何其他定制配置信息。 我们使用此元素来存储服务的用户和密码凭据。 使用ConfigurationSettings库以这种方式在代码中访问元素中指定的键/值对即ConfigurationManager.AppSettings [“ AVeryBigSum_User”]。 appSettingsadd keyaspnet:UseTaskFriendlySynchronizationContext valuetrue/add keyAVeryBigSum_User valueAVeryBigSum/add keyAVeryBigSum_Pass value12345//appSettings 因此我们可以更改这些凭据而无需重建项目的动态链接库文件 DLL。 尽管使用上述元素具有优势但与服务的定义有关的所有魔术都发生在ServiceModel标记的边界中。 使用SoapUI 2保护WCF SOAP –行为 该标签定义了端点和服务分别消耗的协议元素。 服务凭证元素对于定义至关重要。 它指定了身份验证过程中使用的自定义验证模式。 behaviorsserviceBehaviorsbehavior nameDebugModeBehavior!-- To avoid disclosing metadata information, set the values below to false before deployment --serviceMetadata httpGetEnabledtrue httpsGetEnabledtrue/!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --serviceDebug includeExceptionDetailInFaultstrue/!--For UserPass Authentication--serviceCredentialsuserNameAuthentication userNamePasswordValidationModeCustom customUserNamePasswordValidatorTypeAVeryBigSum_BasicAuthentication.ServiceAuthenticator, AVeryBigSum_BasicAuthentication//serviceCredentials/behavior/serviceBehaviors/behaviors 在服务凭证 s中定义了另一个重要属性用于指定用户名和密码验证的设置。 userNamePasswordValidationMode属性设置如何验证凭据。 我们的服务使用自定义类来验证凭据。 此类AVeryBigSum.ServiceAuthenticator可在AVeryBigSum项目中找到。 使用SoapUI的3-安全WCF SOAP –绑定 WCF服务中的每个终结点都需要明确指定绑定。 绑定由绑定元素的有序堆栈组成每个绑定元素指定连接到服务端点所需的一部分通信信息。 如我们所见我们正在使用WSHttpBinding。 它表示可互操作的绑定该绑定支持分布式事务安全可靠的会话。 bindingswsHttpBinding!-- configure wsHttp binding with Transport security mode and clientCredentialType as Certificate --binding namewsHttpBinding_LargeBinding closeTimeout00:01:00 openTimeout00:01:00 receiveTimeout00:10:00 sendTimeout00:10:00 bypassProxyOnLocalfalsetransactionFlowfalse hostNameComparisonModeStrongWildcard maxBufferPoolSize250000000 maxReceivedMessageSize250000000 messageEncodingTexttextEncodingutf-8 useDefaultWebProxytrue allowCookiesfalsereaderQuotas maxDepth2000000 maxStringContentLength2147483647 maxArrayLength2147483647 maxBytesPerRead2147483647 maxNameTableCharCount2147483647/reliableSession orderedtrue inactivityTimeout00:10:00 enabledfalse/!--For UserPass Authentication--security modeTransportWithMessageCredentialmessage clientCredentialTypeUserName establishSecurityContextfalse//security/binding/wsHttpBinding/bindings WSHttpBinding使用HTTP传输并提供消息安全性事务可靠的消息传递和WS-Addressing它们是默认启用的也可以通过单个控件设置使用。 在WSHttpBinding元素内部我们将安全模式定义为TransportWithMessageCredential。 传输确定提供传输级别安全性的实际机制。 对于HTTP该机制是基于HTTPHTTPS的安全套接字层SSL 使用SoapUI的4种安全WCF SOAP –服务 最后在服务元素上我们定义了终结点公开了服务元数据。 发布元数据如描述服务使用的所有方法和数据类型的Web服务描述语言WSDL文档很有用。 SoapUi将在此传奇的下一篇文章中使用它来检索和调用所有可服务的端点。 servicesservice behaviorConfigurationDebugModeBehavior nameAVeryBigSum_BasicAuthentication.Avbsendpoint addressendpointAVeryBigSum_BasicAuthentication bindingwsHttpBindingbindingConfigurationwsHttpBinding_LargeBinding nameEndpointAVeryBigSum_BasicAuthenticationcontractAVeryBigSum_BasicAuthentication.IAvbs /endpoint addressmex bindingwsHttpBinding bindingConfigurationwsHttpBinding_LargeBindingnamemexEndpoint contractIMetadataExchange //service/services使用SoapUI的5种安全WCF SOAP –自定义验证器类 定制验证器扩展了UserNamePasswordValidator类并覆盖了Validate方法。 该验证器在“服务行为”中定义为默认授权管理器如上面行为部分中所示。 此类将客户端调用接收到的信息与AppsSetting元素中定义的信息进行比较。 using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IdentityModel.Selectors;
using System.Linq;
using System.ServiceModel;
using System.Web;
namespace AVeryBigSum_BasicAuthentication
{public class ServiceAuthenticator : UserNamePasswordValidator{public override void Validate(string userName, string password){if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))throw new SecurityTokenException(Username and password required);if (!(userName ConfigurationManager.AppSettings[AVeryBigSum_User] amp;amp; password ConfigurationManager.AppSettings[AVeryBigSum_Pass]))throw new FaultException(string.Format(Wrong username ({0}) or password , userName));}}
} 为了使此类正常工作我们需要在项目中添加两个外部库。 我们可以通过右键单击项目- Manage NuGet Packages浏览Microsoft.IdentityModel.Logging和Microsoft.IdentityModel.Tokens软件包并添加两者来完成此操作。 现在我们已经定义了端点接下来我们将服务托管在本地开发服务器中。 部署并运行服务 1 –要将我们的服务托管在IIS中请右键单击该项目然后转到“ 属性” 。 在属性窗口中选择“ Web”选项卡。 2-现在在“ Web上的服务器”设置下您将看到以下详细信息将“ IIS Express”更改为“ IIS Server”。 3 –现在单击使用以管理员身份运行的Visual Studio创建虚拟目录。 您将收到一条消息虚拟目录已成功创建 否则您将收到一条错误消息并且需要以管理员身份再次启动Visual Studio。 现在按F5键您的应用程序将在IIS服务器而不是IIS express上启动并运行。 结论 在本演示结束时我们将提供一个由SoapUI调用的安全服务。 我们的下一篇文章将逐步演示如何做到这一点。 另外可以从GitHub存储库访问该示例 要下载它请点击此链接 。 翻译自: https://www.javacodegeeks.com/2020/04/invoking-different-secure-wcf-soap-services-using-soapui-basic-authentication-part-i.html