Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this endpoint in my app.config but the question is:
How can I set the user and password from UsernameToken by code?

XML
<endpoint address="https://10.235.135.219/soap/" binding="basicHttpBinding" bindingConfiguration="PlacetoPay_EfectyBinding"    contract="ServicioPlaceToPay.PlacetoPay_EfectyPort"name="PlacetoPay_EfectyPort">
   <headers>
      <wsse:Security>
        <wsse:UsernameToken>
           <wsse:Username>juan</wsse:Username>
           <wsse:Password>j321*</wsse:Password>
        </wsse:UsernameToken>
      </wsse:Security>
   </headers>
</endpoint>


I tried with the next example but it doesn't work:

C#
client.ClientCredentials.UserName.UserName = "juan";
client.ClientCredentials.UserName.Password = "j321*";


Thanks for your answers.
Posted
Updated 12-Jan-16 4:02am
v2

It can be done at runtime but really it should be set outside the main application. You can do it by:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["UserName"].Value = "myUserName";
config.Save(ConfigurationSaveMode.Modified);


More details below:
ConfigurationManager Class (System.Configuration)[^]
 
Share this answer
 
Comments
judah9107 13-Jan-16 7:31am    
This way just could modify the "appsettings" section, but I need modify the
endpoint-headers section
I got the solution:

C#
ServicePointManager.ServerCertificateValidationCallback =
                        (object s, X509Certificate certificate, X509Chain chain,
                                         SslPolicyErrors sslPolicyErrors) => true;
//STEP 1: Create Binding
SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.IncludeTimestamp = false;
TextMessageEncodingBindingElement encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
HttpsTransportBindingElement transportElement = new HttpsTransportBindingElement();

CustomBinding customBinding = new CustomBinding(securityElement, encodingElement, transportElement);

//STEP 2: Create endpointAddress
EndpointAddress endpoint = new EndpointAddress("https://10.235.135.219:8443/soap/efecty/");

//STEP 3: Create Client and associate the "binding" and the "endpoint" to client
ServicioPlaceToPay.PlacetoPay_EfectyPortClient client = new PlacetoPay_EfectyPortClient(customBinding, endpoint);

//STEP 4: Add client credentials 
client.ClientCredentials.UserName.UserName = "juan";
client.ClientCredentials.UserName.Password = "j321*";                                                                                       

//STEP 5: Invoke the method and check if now it's working
ServicioPlaceToPay.BillResponse a = client.getBillByReference("1248");
 
Share this answer
 
Comments
Abdul Rahman El Habboub 18-Jan-22 11:48am    
@judah9107

Thanks for your solution, but any idea why it would throw the following exception:
System.InvalidOperationException: 'The CustomBinding on the ServiceEndpoint with contract 'MyContract' lacks a TransportBindingElement. Every binding must have at least one binding element that derives from TransportBindingElement.'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900