Hi All,
I am trying to activate synchronous AIF and produce a salesOrder web service to call it in C#.NET. It is working fine by default network user logon but I need to logon to AX with another network user who is also valid in active directory and Dynamics Ax users whereas not logon to as current windows user. In the other word, I need to send UserName and Password to IIS and further to AX to authenticate.
I think something has to be done in preparing address header(setEndpointUser()) but not sure!
This is my code which is read a record from salesOrder finely by default logon user in windows. I am new in IIS and no idea to how to send a username and password to AX.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysColl = System.Collections;
using SysSvcmod = System.ServiceModel;
using SysSvcmodChan = System.ServiceModel.Channels;
using SysXml = System.Xml;
using AifSos = ConsoleApplication1.SO;
namespace ConsoleApplication1
{
class ProgramClass
{
protected AifSos.SalesOrderServiceClient m_salesOrderServiceClient;
static void Main(string[] args)
{
ProgramClass programClass;
AifSos.SalesOrderServiceClient salesOrderServiceClient;
try
{
salesOrderServiceClient = new AifSos.SalesOrderServiceClient();
programClass = new ProgramClass(salesOrderServiceClient);
programClass.SendMessage();
Console.ReadKey();
}
catch (Exception excepn)
{
Console.Out.WriteLine(excepn.ToString());
Console.ReadKey();
}
}
protected ProgramClass(AifSos.SalesOrderServiceClient salesOrderServiceClient)
{
this.m_salesOrderServiceClient = salesOrderServiceClient;
}
protected void SendMessage()
{
AifSos.QueryCriteria queryCriteria;
SysSvcmod.OperationContextScope operContextScope;
try
{
operContextScope = new SysSvcmod.OperationContextScope(m_salesOrderServiceClient.InnerChannel);
using (operContextScope)
{
this.prepareHeader(“bc1”, @“GBGNetwork\A.Karimzadeh”, “PDA”);
queryCriteria = this.PrepareBody_QueryCriteria();
this.CallAifSalesOrderService_Find(queryCriteria);
}
}
catch (Exception excepn)
{
Console.Out.WriteLine(excepn.ToString());
}
}
private void prepareHeader(string destinationEndpoint, string sourceEndpointUser, string sourceEndpoint)
{
this.setHeaderDestinationEndpoint(destinationEndpoint);
this.setEndpointUser(sourceEndpointUser, sourceEndpoint);
this.setHeaderMsgId();
}
private void setHeaderMsgId()
{
SysXml.UniqueId guidUniqueId = new SysXml.UniqueId(Guid.NewGuid());
SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.MessageId = guidUniqueId;
}
private void setHeaderDestinationEndpoint(string destinationEndpoint)
{
System.ServiceModel.Channels.MessageHeader messageHeader;
messageHeader = System.ServiceModel.Channels.MessageHeader.CreateHeader(“DestinationEndpoint”,
"">schemas.microsoft.com/…/services",
destinationEndpoint);
SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
}
private void setEndpointUser(string sourceEndpointUser, string sourceEndpoint)
{
SysSvcmodChan.AddressHeader addressHeader;
SysSvcmod.EndpointAddressBuilder addressBuilder;
SysSvcmod.EndpointAddress endpointAddress;
addressHeader = SysSvcmodChan.AddressHeader.CreateAddressHeader(“SourceEndpointUser”,
"">schemas.microsoft.com/…/services",
sourceEndpointUser);
endpointAddress = new SysSvcmod.EndpointAddress(new Uri(“urn:” + sourceEndpoint), addressHeader);
addressBuilder = new SysSvcmod.EndpointAddressBuilder(endpointAddress);
SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.From = addressBuilder.ToEndpointAddress();
}
protected AifSos.QueryCriteria PrepareBody_QueryCriteria()
{
AifSos.QueryCriteria queryCriteria;
AifSos.CriteriaElement[] criteriaElements;
criteriaElements = new AifSos.CriteriaElement[1];
criteriaElements[0] = new AifSos.CriteriaElement();
criteriaElements[0].DataSourceName = “SalesTable”;
criteriaElements[0].FieldName = “SalesId”;
criteriaElements[0].Operator = AifSos.Operator.Range;
criteriaElements[0].Value1 = “SO-005517”;
criteriaElements[0].Value2 = “SO-005517”;
queryCriteria = new AifSos.QueryCriteria();
queryCriteria.CriteriaElement = criteriaElements;
return queryCriteria;
}
protected void CallAifSalesOrderService_Find(AifSos.QueryCriteria queryCriteria)
{
AifSos.AxdSalesOrder axdSalesOrder;
AifSos.AxdEntity_SalesTable axdEntity_SalesTable;
AifSos.AxdEntity_SalesLine axdEntity_SalesLine;
SysColl.IEnumerator enumerSalesTable, enumerSalesLine;
int iCountLoops1 = 0, iCountLoops2;
// Issue the Find.
axdSalesOrder = this.m_salesOrderServiceClient.find(queryCriteria);
// Loop through the results.
enumerSalesTable = axdSalesOrder.SalesTable.GetEnumerator();
while (enumerSalesTable.MoveNext())
{
++iCountLoops1;
axdEntity_SalesTable = (AifSos.AxdEntity_SalesTable)enumerSalesTable.Current;
Console.Out.WriteLine(Environment.NewLine + “SalesTable[” + iCountLoops1 + "]: SalesId = " + axdEntity_SalesTable.SalesId
- " , CustAccount = " + axdEntity_SalesTable.CustAccount + " , CustGroup = " + axdEntity_SalesTable.CustGroup);
// Loop through the child rows.
if (null != axdEntity_SalesTable.SalesLine)
{
enumerSalesLine = axdEntity_SalesTable.SalesLine.GetEnumerator();
iCountLoops2 = 0;
while (enumerSalesLine.MoveNext())
{
++iCountLoops2;
axdEntity_SalesLine = (AifSos.AxdEntity_SalesLine)enumerSalesLine.Current;
Console.Out.WriteLine(" ** SalesLine[" + iCountLoops2 + "]: LineAmount = " + axdEntity_SalesLine.LineAmount);
} // End loop: SalesLine (child)
}
} // End loop: SalesTable (parent)
}
}
}