0

I am new to .netcore, I am working on web api that are running on docker container and while using postman the web api's are working really fine outputting the results. I want to make a program in .netcore calling the webapi endpoints and getting the response and using that particular response in other endpoints with MVC.

The explanation is given below. The default username and password for admin is default set for example username:admin , password: helloworld . The first time admin login the api requires a new personal password as shown in the Postman figure below.

The login api is: localhost://..../v1/users/login Login Admin

The first question is How to give the values in Authorization->BasicAuth using .netcore. The body of the api looks like the figure below. Setting new_password for Admin

After setting the new_password the response of the api is a token as given below. Token Generated

The particular token is then use in the Environment to create user. The image for more clear problem is given below. Token Save

Lastly, the token then used to make other API calls such as creating a user. API: https://localhost/..../v1/users The image is below. Creating a User

As a newbie in .netcore language, I am really struggling to do this kind of API calls, as most of the tutorials I tried are generating their own token from API, but here I just want to take the response token and save it and then use it in other API calls. The StackOverflow community's support was always really handy for me.

The Code I'm trying is given below.

**Controller**

 public class Login_AdminController : ControllerBase
    {
        [Route("/loginAdmin")]
        [HttpPost]
        public async Task<string> LoginAdminAsync([FromBody] dynamic content)
        {
            LoginAdmin L = new LoginAdmin();
            var client = new HttpClient();
            client.BaseAddress = new Uri("https://localhost:9090");
            var request = new HttpRequestMessage(HttpMethod.Post, "/v1/users/login");
            var byteArray = new UTF8Encoding().GetBytes($"<{L.username}:{L.df_Password}>");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

            var formData = new List<KeyValuePair<string, string>>();
            formData.Add(new KeyValuePair<string, string>("new_password", "helloWorld123!"));

            request.Content = new FormUrlEncodedContent(formData);
            var response = await client.SendAsync(request);
            Console.WriteLine(response);
            return content;
        }
   }
}
***Model***
    public class LoginAdmin
    {
        public string username = "admin";
        public string df_Password = "secret";
        public string new_Password { get; set; }
    }

Thank you.

Community
  • 1
  • 1
Farrukh Ahmed Khan
  • 301
  • 1
  • 3
  • 19

1 Answers1

1

It looks like your question is around how to do API Requests in dotnet core. The class you are looking for is called the HttpClient and in dotnet core it underwent some changes in how you can create one. I would recommend you check these docs for how to create one and some learning around it: ASPNET CORE 2.2 HttpRequests

Finally I would recommend this question for an example of posting JSON via the Client you create: POSTing JsonObject With HttpClient From Web API . It's worth noting there are extension methods to pass objects instead of string content, and in the first document you can see examples of adding headers (Such as Authorization)

JEV
  • 2,494
  • 4
  • 33
  • 47
  • Thank you @JonE for your answer. I edit my question with the code I'm trying. Can you check it? – Farrukh Ahmed Khan Jun 21 '19 at 11:26
  • Is the URL you are trying to hit on this same server? If so you don't want to use the HttpClient andw ill want to use: ``` return RedirectToAction("SomeController", "OtherLoginAction", new LoginAdmin() { .. } ); ``` – JEV Jun 21 '19 at 12:57
  • No the url from IISExpress server is different than the one I'm trying to hit. The ```localhost:9090``` is running from docker container, and the .netcore app is running on local machine server. – Farrukh Ahmed Khan Jun 21 '19 at 12:59
  • I wouldn't use that FormEncoded stuff, as that's pretty old and unnecessary look at the Answer I posted (Second link) – JEV Jun 24 '19 at 07:28