|
1 | 1 | using System;
|
2 | 2 | using System.Security.Cryptography.X509Certificates;
|
3 | 3 | using System.ServiceModel;
|
| 4 | +using System.ServiceModel.Channels; |
4 | 5 | using Infrastructure.Common;
|
5 | 6 | using Xunit;
|
6 | 7 |
|
@@ -151,4 +152,66 @@ public static void Https_SecModeTransWithMessCred_UserNameClientCredential_Succe
|
151 | 152 | ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
|
152 | 153 | }
|
153 | 154 | }
|
| 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 | + } |
154 | 217 | }
|
0 commit comments