[SalesForce] Use Named Credentials with a SOAP WSDL2APEX class

SF allowed the usage of Named Credentials with SOAP callouts but the documentation doesn't really go into details of how to pass/access the Username and Password to the stub.inputHttpHeaders_x . I have a webservice that uses basic authentication and I need to pass the details as below.

final String authHttpHeader = 'Basic ' + EncodingUtil.base64Encode( Blob.valueOf( User_Name + ':' + Password ) );
stub.inputHttpHeaders_x.put( 'Authorization', authHttpHeader );
stub.endpoint_x = 'callout:My_Named_Credential'

https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_security_named_credential_web_service_callouts.htm

If some can give an example of how to access the username/password from the named credentials that will be a great help.

Best Answer

I haven't tested this, but when you are creating your Named Credential you have the option to tick "Generate Authorization Header", which should do this for you automatically:

enter image description here