How to implement basic authentication with Dynamics Ax 2012 web services
Important: This sample is not a production ready solution. As described below the user credentials will be passed in plain text which is not ideal in a production environment. To prevent this from occurring it would be recommended to incorporate SSL with this solution.
- Create inbound HTTP port.
- Navigate to System administration->Setup->Services and Application Integration Framework->Inbound ports.
- Create an inbound port using the HTTP adapter with settings similar to the image below.
Basic settings:
Service operations:
- Activate the new inbound port.
- We need to configure the client configuration. In order to do this you need to select the ‘Configure’ button. Note: Do not select the ‘Configure AOS’ button.
- You should see a dialog with a warning. Just click yes to continue loading the configuration file.
- Locate the Bindings section under the Configuration frame and select ‘New Binding Configuration’ from the Bindings frame.
- Select ‘basicHttpBinding’ and click OK.
- In the ‘Name’ field enter ‘basicHttpBindingWithBasicAuth’.
- Click the ‘Security’ tab and set the attributes as shown below.
- Locate the ‘reqReplyEndpoint’ in the Configuration frame, and highlight it.
- Change the BindingConfiguration attribute for the endpoint from ‘basicHttpBindingWithWindowsAuth’ to ‘basicHttpBindingWithBasicAuth’.
- Save the configuration file and close the WCF configuration editor.
- Activate the SalesOrderHTTPService port.
- Open IIS manager and navigate to the MicrosoftDynamicsAXAif60 virtual directory. Highlight it, and from the main frame open ‘Authentication’ from the IIS group.
- Enable Basic Authentication. Your settings should look like that below.
- Close IIS manager and complete an iisreset from a command prompt.
- Open Visual Studio.
- Select File->New->Project. From the New Project dialog select Windows Forms Application. Enter a name for the project (e.g. SalesOrderTest). Click OK.
- Once the project opens drag a button from the toolbox onto the form.
- Add the Sales Order Service reference to the project. Note: You can pull the address from the ‘Inbound ports’ form within Dynamics Ax.
Important: You will be asked for credentials in order to access the service and metadata. I entered my administrator account and password until the RoutingService showed in the Services frame below, then cancelled all remaining authentication dialogs. This will only work if you have Windows and Basic authentication enabled on your IIS box for the AIF virtual directory.
- Close IIS manager. Open a command prompt and complete an iisreset.
- Open Visual Studio and open your project back up.
- Double-click the Button on the form to open the code window and create a new button_click event.
- Add a ‘using’ statement for the sales order web service.
- Within the newly created button click event paste the following code.
SalesOrderServiceClient proxy = new SalesOrderServiceClient();
proxy.ChannelFactory.Credentials.UserName.UserName = @”domain\user”;
proxy.ChannelFactory.Credentials.UserName.Password = @”password”;
CallContext context = new CallContext();
context.Company = “ceu”;
AxdSalesOrder salesOrder = new AxdSalesOrder();
AxdEntity_SalesTable[] salesTables = new AxdEntity_SalesTable[1];
AxdEntity_SalesTable salesTable = new AxdEntity_SalesTable();
salesTable.CurrencyCode = “USD”;
salesTable.CustAccount = “1103”;
salesTable.ReceiptDateRequested = Convert.ToDateTime(“2/1/2012”);
salesTable.Payment = “N060”;
salesTable.PurchOrderFormNum = “PO113”;
#region Financial Dimensions
AxdType_DimensionAttributeValue dimBusinessUnit = new AxdType_DimensionAttributeValue();
dimBusinessUnit.Name = “BusinessUnit”;
dimBusinessUnit.Value = “20”;
AxdType_DimensionAttributeValue dimCustomerGroup = new AxdType_DimensionAttributeValue();
dimCustomerGroup.Name = “CustomerGroup”;
dimCustomerGroup.Value = “10”;
AxdType_DimensionAttributeValue dimDepartment = new AxdType_DimensionAttributeValue();
dimDepartment.Name = “Department”;
dimDepartment.Value = “500”;
AxdType_DimensionAttributeValueSet valueSet = new AxdType_DimensionAttributeValueSet();
valueSet.Values = new AxdType_DimensionAttributeValue[3] { dimBusinessUnit, dimCustomerGroup, dimDepartment };
salesTable.DefaultDimension = valueSet;
#endregion
AxdEntity_SalesLine salesLine = new AxdEntity_SalesLine();
salesLine.ItemId = “1000”;
salesLine.SalesQty = 1;
salesLine.SalesUnit = “ea”;
AxdEntity_SalesLine salesLine2 = new AxdEntity_SalesLine();
salesLine2.ItemId = “1000”;
salesLine2.SalesQty = 55;
salesLine2.SalesUnit = “ea”;
AxdEntity_SalesLine salesLine3 = new AxdEntity_SalesLine();
salesLine3.ItemId = “10004”;
salesLine3.SalesQty = 21;
salesLine3.SalesUnit = “Pcs”;
AxdEntity_InventDim inventDim = new AxdEntity_InventDim();
inventDim.InventSiteId = “1”;
salesLine3.InventDim = new AxdEntity_InventDim[1] { inventDim };
salesTable.SalesLine = new AxdEntity_SalesLine[3] { salesLine, salesLine2, salesLine3 };
salesOrder.SalesTable = new AxdEntity_SalesTable[1] { salesTable };
try
{
proxy.create(context, salesOrder);
MessageBox.Show(“Worked”);
}
catch (Exception ex)
{
throw ex;
}
- Open IIS manager and navigate to the MicrosoftDynamicsAXAif60 virtual directory. Highlight it, and from the main frame open ‘Authentication’ from the IIS group.
- Disable Windows Authentication. Your settings should look like that below.
- Close IIS manager and complete an iisreset from a command prompt.
- Build and test the program.
Note: The data presented in the sample above may not coincide with your existing database. You may have to update the code to validate against your AX setup. If you do not update the sample code you are likely to get a validation error returned from the server.