0

I am trying to load the data (JSON) from API to SQL Sever Database Table. Able to get the response with Test API but when I am unable to authenticate the API with username and Passport.


            {

                string connetionString = null;
                string sql = null;
                string serviceUrl = "https://jsonplaceholder.typicode.com/posts";
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(serviceUrl);
                var serializer = new JavaScriptSerializer();

                

                client.DefaultRequestHeaders.Accept.Add(
                  new MediaTypeWithQualityHeaderValue("application/json"));
                connetionString = "Data Source=BM_BHAKST;Initial Catalog=Practice_OPS; Trusted_Connection=True;";

                string APIUrl = string.Format(serviceUrl);
                var response = client.GetAsync(APIUrl).Result;
                if (response.IsSuccessStatusCode)

                {

                    var result = response.Content.ReadAsStringAsync().Result;



                    var dt = serializer.Deserialize<Dictionary<string,string>[]>(result);

                    using (SqlConnection cnn = new SqlConnection(connetionString))
                        //using (SqlConnection cnn = new SqlConnection(connetionString))
                        //DataTable dataTable = new DataTable();
                        //SqlConnection cnn = new SqlConnection(connetionString);
                    {

I am completely new to c# can anyone help me to implement the same functionality with username & password. (Username : BASIC_Reporting ,Password : Abc12345)

Learner
  • 25
  • 5
  • Are you sure the problem is that production can't login to the rest-services? Another thing could be that the user account running the service in production don't have access to the database. If it is the rest-services, do you have any info about the way you should authenticate, I mean what technique to use? – steb May 25 '22 at 13:17
  • Not with the issue in production. In the above code In the place of Test API .. I need to replace with the original API . But original API consists of Username and Password. Since I am completely new to C# I am unable to modify the above code according to the username and password. – Learner May 25 '22 at 13:21
  • Still we don't know what Authentication scheme is used then. Basic auth? – Ralf May 25 '22 at 13:22
  • Trusted_Connection=True uses Windows Credentials. If you want user name and password change the connection string. – jdweng May 25 '22 at 13:34
  • @Ralf it's basic authetication. In the above c# code I have the API with out username and password .... If I am calling the API with username and password in the code where do I need to change – Learner May 25 '22 at 13:41
  • Your question requires some error or problem description. – Kieran Foot May 25 '22 at 16:26
  • You need to set the Authorization header for your request. Since you already fiddle with the DefaultRequestHeaders you can set it via that like shown here https://stackoverflow.com/questions/57701823/how-do-i-send-an-http-post-with-http-basic-authorization-in-asp-net-core – Ralf May 25 '22 at 17:16

0 Answers0