AX – D365FO – Set credentials in AIF Service client / Web Service

This code shows how to pass user and password to a Web Service from AX

System.ServiceModel.Description.ClientCredentials               clientCredentials;
System.ServiceModel.Security.UserNamePasswordClientCredential   userNamePasswordClientCredential;
System.ServiceModel.BasicHttpBinding                            basicHttpBinding;
System.ServiceModel.BasicHttpSecurity                           basicHttpSecurity;
System.ServiceModel.BasicHttpSecurityMode                       basicHttpSecurityMode;
System.ServiceModel.HttpTransportSecurity                       httpTransportSecurity;
System.ServiceModel.HttpClientCredentialType                    httpClientCredentialType;
System.ServiceModel.EndpointAddress                             endpointAddress;
System.ServiceModel.Description.ServiceEndpoint                 endpoint;
SiemensT4EAImport.T4EAImportService.DefaultPortTypeClient       serviceClient;
System.Type                                                     serviceClientType;

serviceClientType = CLRInterop::getType('T4EAImport.ImportService.DefaultPortTypeClient');
serviceClient = AifUtil::createServiceClient(serviceClientType);

basicHttpSecurityMode    = System.ServiceModel.BasicHttpSecurityMode::TransportCredentialOnly;
httpClientCredentialType = System.ServiceModel.HttpClientCredentialType::Basic;

httpTransportSecurity = new System.ServiceModel.HttpTransportSecurity();
httpTransportSecurity.set_ClientCredentialType(httpClientCredentialType);

basicHttpSecurity = new System.ServiceModel.BasicHttpSecurity();
basicHttpSecurity.set_Mode(basicHttpSecurityMode);
basicHttpSecurity.set_Transport(httpTransportSecurity);

basicHttpBinding = new System.ServiceModel.BasicHttpBinding();
basicHttpBinding.set_Security(basicHttpSecurity);

endpointAddress = new System.ServiceModel.EndpointAddress("http://xxx.xxx.com:xxxx/soap2/t4eaimportjobservice");

endpoint = serviceClient.get_Endpoint();
endpoint.set_Binding(basicHttpBinding);
endpoint.set_Address(endpointAddress);

clientCredentials = serviceClient.get_ClientCredentials();
userNamePasswordClientCredential = clientCredentials.get_UserName();
userNamePasswordClientCredential.set_UserName(username);      //Set username
usernamePasswordClientCredential.set_Password(password);      //Set password

serviceClient.ImportJob(itemId, itemRevisionId, axUserEmail, byRef message, byRef code);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s