我正在 C# 中制作 WCF 应用程序。这对我来说是全新的,我不确定我在寻找什么,但我会尽力解释它。我在 SOAP 消息中插入安全标头时遇到问题。这是我的 App.config 文件`
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="FleetGrip" connectionString="data source=srv-gc-sqldev02.unitas.mobsat.local\sql2012dev2;initial catalog=FleetGrip;persist security info=True;user id=FleetGripUser;password=123;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceBoxAdministrationImplServiceSoapBinding"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="1048576000"
maxBufferPoolSize="524288"
maxReceivedMessageSize="1048576000"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="819200"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode ="None">
<transport clientCredentialType="Basic"
proxyCredentialType="None"
realm="None" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://bovbox-ipns.vas.vizada.com/WS_VBOX_BO/soap/serviceBoxAdministration"
binding="basicHttpBinding" bindingConfiguration="ServiceBoxAdministrationImplServiceSoapBinding"
contract="BoxAdminServiceReference.ServiceBoxAdministrationImpl"
name="ServiceBoxAdministrationImplPort" />
</client>
</system.serviceModel>
</configuration>
这是我在代码中尝试过的
namespace TesterTool.DataService
{
class ServiceLayerClass : INotifyPropertyChanged
{
// ViewModel WCf
public event PropertyChangedEventHandler PropertyChanged;
public void CreateBox(string Username, string Password, string BoxID, string SoftwareId)
{
var client = new BoxAdminServiceReference.ServiceBoxAdministrationImplClient();
client.ClientCredentials.UserName.UserName = "adminvizada sas_dev2";
client.ClientCredentials.UserName.Password = "k2oURgx4PkXs";
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var h =
"<UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><Username>adminvizada sas_dev2</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">k2oURgx4PkXs</Password></UsernameToken>";
var escaped = System.Security.SecurityElement.Escape(h);
var header =
new MessageHeader<string>(h);
var untypedHeader = header.GetUntypedHeader("Security",
System.Security.SecurityElement.Escape("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"));
OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader/*new CustomMessageHeader()*/);
var response = client.createBox("1215-100010", "4.0.2");
Console.ReadLine();
}
}