You need to create a azure app and enable permission to use the crm environment. 
App Id is the "azure app" id that you have created in azure. 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net;
using System.Threading.Tasks;
using System;
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Crm.Sdk.Messages;
class program
{
    static void Main(string[] args)
    {
        string url = "https://org72e95421.crm.dynamics.com";
        string userName = "user@5521.onmicrosoft.com";
        string password = "pass@123";
        string conn = $@"
    Url = {url};
    AuthType = OAuth;
    UserName = {userName};
    Password = {password};
    AppId = 77c8a4be-1cf1-4022-8ff9-f03c9e88b3b3;     
    RedirectUri = https://localhost;
    LoginPrompt=Auto;
    RequireNewInstance = True";
        using (var svc = new CrmServiceClient(conn))
        {
 if (svc.IsReady)
            {
                var myContact = new Entity("contact");
                myContact.Attributes["lastname"] = "Test";
                myContact.Attributes["firstname"] = "ram";
                svc.Create(myContact);
                Console.WriteLine("created contact");
            }
            WhoAmIRequest request = new WhoAmIRequest();
            WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request);
            Console.WriteLine("Your UserId is {0}", response.UserId);
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }
}
No comments:
Post a Comment