I am trying to create a SOAP service with code unit and performing a simple scenario of creating sales order header. I am unable to create sales order as I am calling my API from console application. Error when I run my console app code:
An unhandled exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll
Additional information: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="", Bearer authorization_uri="https://login.windows.net/......75668f36-65d37.../oauth2/authorize"'.
If I run same code unit on a trigger it works fine
Here is my code
Code unit:
codeunit 50131 SalesOrderCodeUnit
{
trigger OnRun()
begin
Create();
end;
[ServiceEnabled]
procedure Create()
var
SalesHeader: Record "Sales Header";
Document_Type: Enum "Sales Document Type";
Sell_to_Customer_No: Code[20];
Sell_to_Customer_Name: Text[100];
begin
Document_Type := SalesHeader."Document Type"::Order;
Sell_to_Customer_No := '30000';
Sell_to_Customer_Name := 'School of Fine Art';
Clear(SalesHeader);
SalesHeader.Init();
SalesHeader.Validate("Document Type", SalesHeader."Document Type"::Order);
SalesHeader.Validate("Sell-to Customer No.", Sell_to_Customer_No);
SalesHeader.Validate("Sell-to Customer Name", Sell_to_Customer_Name);
SalesHeader.Insert(true);
end;
}
console app code:
ServiceReference1.SalesOrderCodeUnit_PortClient portClient = new ServiceReference1.SalesOrderCodeUnit_PortClient();
portClient.ClientCredentials.UserName.UserName = "DANYAL.BUTT";
portClient.ClientCredentials.UserName.Password = "....qInECuJqR2Q........";
portClient.Create();
Console.WriteLine();
I am using SaaS and yes web service is OK.
Thank you for the response, I actually solved this issue by adding below line in my .net method
portClient.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
and a change in app.config by
<basicHttpBinding>
<binding name="SalesOrderCodeUnit_Binding">
<security mode="Transport">
<transport clientCredentialType="Basic"></transport>
</security>
</binding>
<binding name="SalesOrderCodeUnit_Binding1" />
</basicHttpBinding>
Hi,
Is NAV / BC running OnPrem or in SaaS? Meaning are you able to change the authentification methods. And if you are using Visual Studio to create your console app, do you see the web service OK from there?
Sorry to only have questions for now.
Sohail Ahmed
1,200
YUN ZHU
1,006
Super User 2025 Season 1
Mansi Soni
836