AIF Web Service custom Username and password login

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)

}

}

}

Hope this might help you out.

var binding = new BasicHttpBinding();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
var address = new EndpointAddress(new Uri("http://dynamicsserver/salesorderservice.svc"));
var client = new SalesOrderServiceClient(binding, address);
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("admin", "pass", "domain);

Hi Shantram,

Thank you very much for the Code.

I have imported it but got the following error in my console:

“ystem.ServiceModel.FaultException’1[consoleApplicatoin.SO.AIFFault]:Request Failed. See Wxception Log for details. .”

And this the Exception window

The username you are specifying might not be business proxy account. (Business Connector Proxy Account)

I guess, in AIF its a requirement .

You need to check some AIF Documentation for the same.

Regards,

Shantaram

So Many Thanks Shantaram! The Problem was Solved, I had to change some settings in web.config.

Please mark the question as resolved.

Dear Shantaram,

I faced with another problem now. I don’t know how can I authenticate the user which are valid in Endpoints.

Actually I think before instantiating SalesOrderServiceClient the System.ServiceModel.EndpointAddress must be configured

firstly with System.ServiceModel.channel.MessageHeader to LOCAL ENDPOINT,

secondly with System.ServiceModel.channel.AddressHeader to NETWORK\USERNAME

and thirdly System.ServiceModel.EndpointAddress to ENDPOINT NAME

Similar to code below:

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”,

http://schemas.microsoft.com/dynamics/2008/01/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”,

http://schemas.microsoft.com/dynamics/2008/01/services”,

sourceEndpointUser);

endpointAddress = new SysSvcmod.EndpointAddress(new Uri(“urn:” + sourceEndpoint), addressHeader);

addressBuilder = new SysSvcmod.EndpointAddressBuilder(endpointAddress);

SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.From = addressBuilder.ToEndpointAddress();

}

The problem is the preparing of this header needs and instance of SalesOrderServiceClient whereas the EndpointAddress should be passed when we new SalesOrderServiceClient.

I really appreciate if you guide me to solve this problem.