Skip to content

Commit b90ebc9

Browse files
authored
Expose EnableUnsecuredResponse in SecurityBindingElement and add unit test (#5176)
* Expose EnableUnsecuredResponse in SecurityBindingElement and add unit test * Add scenario test.
1 parent 5b5a784 commit b90ebc9

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/System.Private.ServiceModel/tests/Scenarios/Binding/WS/TransportWithMessageCredentialSecurity/BasicHttpTransportWithMessageCredentialSecurityTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Security.Cryptography.X509Certificates;
33
using System.ServiceModel;
4+
using System.ServiceModel.Channels;
45
using Infrastructure.Common;
56
using Xunit;
67

@@ -151,4 +152,66 @@ public static void Https_SecModeTransWithMessCred_UserNameClientCredential_Succe
151152
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
152153
}
153154
}
155+
156+
[WcfTheory]
157+
[Condition(nameof(Root_Certificate_Installed),
158+
nameof(SSL_Available))]
159+
[OuterLoop]
160+
[InlineData(true)]
161+
[InlineData(false)]
162+
public static void Https_InvalidClientCredential_EnableUnsecuredResponse_DifferentException(bool enableUnsecuredResponse)
163+
{
164+
EndpointAddress endpointAddress = null;
165+
string testString = "Hello";
166+
string username = null;
167+
string password = null;
168+
ChannelFactory<IWcfService> factory = null;
169+
IWcfService serviceProxy = null;
170+
TransferMode transferMode = TransferMode.Buffered;
171+
try
172+
{
173+
// *** SETUP *** \\
174+
TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.Soap11 };
175+
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement() { TransferMode = transferMode };
176+
TransportSecurityBindingElement sec = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
177+
sec.EnableUnsecuredResponse = enableUnsecuredResponse;
178+
CustomBinding customBinding = new CustomBinding(sec, textEncoding, httpsTransport);
179+
endpointAddress = new EndpointAddress(new Uri(Endpoints.BasicHttps_SecModeTransWithMessCred_ClientCredTypeUserName + $"/{Enum.GetName(typeof(TransferMode), transferMode)}"));
180+
factory = new ChannelFactory<IWcfService>(customBinding, endpointAddress);
181+
username = Guid.NewGuid().ToString("n").Substring(0, 8);
182+
char[] usernameArr = username.ToCharArray();
183+
Array.Reverse(usernameArr);
184+
password = new string(usernameArr);
185+
factory.Credentials.UserName.UserName = username;
186+
factory.Credentials.UserName.Password = password + "1";//invalid password
187+
188+
serviceProxy = factory.CreateChannel();
189+
190+
// *** EXECUTE *** \\
191+
string result = serviceProxy.Echo(testString);
192+
193+
// *** VALIDATE *** \\
194+
Assert.Fail("should throw exception earlier");
195+
196+
// *** CLEANUP *** \\
197+
((ICommunicationObject)serviceProxy).Close();
198+
factory.Close();
199+
}
200+
catch (Exception ex)
201+
{
202+
if (enableUnsecuredResponse)
203+
{
204+
Assert.True(ex is System.ServiceModel.Security.SecurityAccessDeniedException);
205+
}
206+
else
207+
{
208+
Assert.True(ex is System.ServiceModel.Security.MessageSecurityException);
209+
}
210+
}
211+
finally
212+
{
213+
// *** ENSURE CLEANUP *** \\
214+
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
215+
}
216+
}
154217
}

src/System.ServiceModel.Primitives/ref/System.ServiceModel.Security.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ internal SecurityBindingElement() { }
8989
public override T GetProperty<T>(System.ServiceModel.Channels.BindingContext context) { return default; }
9090
public override string ToString() { return default; }
9191
public System.ServiceModel.Security.SecurityKeyEntropyMode KeyEntropyMode { get { return default;} set { } }
92+
public bool EnableUnsecuredResponse { get { return default; } set { } }
9293
}
9394
public enum SecurityHeaderLayout
9495
{

src/System.ServiceModel.Security/tests/ServiceModel/SecutityBindingElementTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public static void Property_KeyEntropyMode()
4343
Assert.Equal(SecurityKeyEntropyMode.CombinedEntropy, securityBindingElement.KeyEntropyMode);
4444
}
4545

46+
[WcfFact]
47+
public static void Property_EnableUnsecuredResponse()
48+
{
49+
//default value in derived class
50+
TransportSecurityBindingElement securityBindingElement = new TransportSecurityBindingElement();
51+
Assert.False(securityBindingElement.EnableUnsecuredResponse);
52+
53+
//initializable from derived class ctor
54+
securityBindingElement = new TransportSecurityBindingElement() { EnableUnsecuredResponse = true};
55+
Assert.True(securityBindingElement.EnableUnsecuredResponse);
56+
57+
//property settable
58+
securityBindingElement.EnableUnsecuredResponse = false;
59+
Assert.False(securityBindingElement.EnableUnsecuredResponse);
60+
}
61+
4662
[WcfFact]
4763
public static void Method_CreateIssuedTokenOverTransportBindingElement()
4864
{

0 commit comments

Comments
 (0)