web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

Create sales order SOAP service

(0) ShareShare
ReportReport
Posted on by 259

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 have the same question (0)
  • Lars Lohndorf-Larsen Profile Picture
    on at

    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.

  • Danyal Profile Picture
    259 on at

    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>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans