'Ws-Trust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication

In Current Project i am using this syntax to retrieve data from Dynamic CRM:

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, null, _crmCredentials, null)
{
    // Creating IOrganizationService object to access organization services
    IOrganizationService service = serviceProxy;
    EntityCollection retrieveAccountGuid = service.RetrieveMultiple(QueryOptions);

    return retrieveAccountGuid;
}

Any one please help me what changes i need to do?

I have Read this article but how to replace IOrganizationService in code?

https://docs.microsoft.com/en-us/power-apps/developer/data-platform/authenticate-office365-deprecation



Solution 1:[1]

public CrmServiceClient devuelve_Servicio()
    {           
        CrmServiceClient svc = null;

        
        string ConnectionString = "AuthType = OAuth; " +
              "Username = ;" +
              "Password = ; " +
              "Url = https://.crm.dynamics.com;" +
              "AppId=;" +
              "RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
              "LoginPrompt=Auto";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        try
        {
            
            CrmServiceClient svc = new CrmServiceClient(ConnectionString);
            if (svc != null)
            {

                this.strErrMsg  = "Error en la conexión!!!" + "LastError: " + svc.LastCrmError.ToString();
               
               }
        }
        catch (Exception ex)
        {

            this.strErrMsg = ex.Message;
        }
        return svc;
    }
}

Solution 2:[2]

The CrmServiceClient is the class we can use to connect to Dynamics 365 in .NET. It supports multiple authentication scenarios. For server-to-server communications on Azure and the Power Platform working with app registrations is the recommended approach.

App registrations accessing Dynamics/Dataverse need to be added as application users and must be assigned an appropriate security role. See also Register an app with Azure Active Directory - MS Docs.

CrmServiceClient implements the IOrganizationService interface, so the majority of your code should work just the same way. Just replace the instantation of the OrganizationServiceProxy by something like this:

private void Test(Uri dataverseUrl, Guid clientId, string clientSecret, string tokenCachePath)
{
    using (var client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(dataverseUrl, clientId.ToString("D"), clientSecret, false, tokenCachePath))
    {
        Perform(client);
    }
}

private void Perform(IOrganizationService organizationService)
{

}

CrmServiceClient can be found in NuGet package Microsoft.CrmSdk.XrmTooling.CoreAssembly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamín Vasquez
Solution 2 Henk van Boeijen