0

I've got an existing Web API backend that uses OAuth to authenticate a vue.js frontend call's. This is an existing one and I can't modify it. I need to perform the authentication from a new WPF Application I wrote.

I've composed the query using the HttpClient in the form

http://backend/api/signin?grant_type=password&username=user&password=1234hola

but I receive an error regarding the grant_type. Is there a tutorial I can follow? I didn't think it was that difficult to perform the authentication, but I think I'm missing something really stupid

Thanks in advance

advapi
  • 3,661
  • 4
  • 38
  • 73
  • Did you try to add the credentials to the header as suggested [here](https://stackoverflow.com/questions/30858890/how-to-use-httpclient-to-post-with-authentication)? How does your vue.js front-end call the service? – mm8 Sep 16 '19 at 12:35
  • @mm8 no, I've not added any header... It's an existing code, not mine.. I've not took a look at what's passing as credential.Give a check as soon as I've got a second, thanks for the hint – advapi Sep 16 '19 at 12:40
  • @mm8 this works, if you put it as answer, I vote as solution – advapi Sep 23 '19 at 10:32

1 Answers1

1

You should add the credentials to the header as suggested here:

var client = new HttpClient() { BaseAddress = new Uri("http://url.com") };
var request = new HttpRequestMessage(HttpMethod.Post, "/path");

var byteArray = new UTF8Encoding().GetBytes("<clientid>:<clientsecret>");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
...
var response = await client.SendAsync(request);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • excuse me @mm8, I've successfully managed it to perform authentication, but how can I save the token for subsequent calls? – advapi Dec 09 '19 at 22:05
  • 1
    @advapi: Please ask a new question if you have another issue. – mm8 Dec 10 '19 at 12:20