C
C#•3y ago
memw

How do i do the following with AuthenticationHeaderValue class?

So i basically wanna auth to github api with the basic authentication the string i'm inputing is actually user:token, but it won't work either, here is what i tried so far.
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(Client.Env["GITCREDS"])));
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(Client.Env["GITCREDS"])));
Client.Env["GITCREDS"] actually returns the string.
36 Replies
memw
memwOP•3y ago
i always get 403 because for some reason i'm not able to auth, i want to remark that with insomnia these creds work
Yawnder
Yawnder•3y ago
@Memw Have you tried breaking it down into steps and see what fails rather than embedding 3 method calls and an indexer?
memw
memwOP•3y ago
yeah i logged Convert.ToBase64String(thing) before and it gave a weird string, i'm guessing comming from Encoding.ASCII.GetBytes()
Yawnder
Yawnder•3y ago
@Memw And when you compared that value from the one shows encoded in the tool you took the screenshot from?
memw
memwOP•3y ago
What do you mean by comparing? it is different that's all i know
Yawnder
Yawnder•3y ago
Well you don't see keen on sharing any details so good luck!
memw
memwOP•3y ago
i do but what details do you want me to share i didn't understand the question correctly, i'm not native english, so sorry for that
Yawnder
Yawnder•3y ago
Alright. Since you shouldn't share a password, try with username abc and password def1%& and give the values of when extracted from the setting and when encoded. I'll compare to what I have on my side.
memw
memwOP•3y ago
With this input
Encoding.ASCII.GetBytes("abc:def1%&")
Encoding.ASCII.GetBytes("abc:def1%&")
The output is YWJjOmRlZjElJg== @Yawnder
Yawnder
Yawnder•3y ago
Same value as I have...
memw
memwOP•3y ago
i'm actually doing this
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("abc:def1%&")));
Console.WriteLine(requestClient.DefaultRequestHeaders.Authorization.Parameter);
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("abc:def1%&")));
Console.WriteLine(requestClient.DefaultRequestHeaders.Authorization.Parameter);
requestClient being an HttpClient
Yawnder
Yawnder•3y ago
Are you certain your Client.Env["GITCREDS"] returns the proper value?
memw
memwOP•3y ago
i logged it and yeah i'm pretty sure it does
Yawnder
Yawnder•3y ago
Humn...
memw
memwOP•3y ago
memw
memwOP•3y ago
i can't share the full thing but yeah it does log the thing and the thing is always
Exception: Response status code does not indicate success: 403 (Forbidden).
Exception: Response status code does not indicate success: 403 (Forbidden).
as if there were no credentials or something
Yawnder
Yawnder•3y ago
Could that be it?
Yawnder
Yawnder•3y ago
Wait, 403 isn't that actually
memw
memwOP•3y ago
I'm using username and token
Yawnder
Yawnder•3y ago
403 means the authentication is working fine, but authorization isn't.
memw
memwOP•3y ago
and with insomnia http client it just gives me 200
Yawnder
Yawnder•3y ago
For the same endpoint?
memw
memwOP•3y ago
yup maybe the way is not user:token? and other thing? or github doesn't want base64? but with raw string doesn't let me either so i'm kinda lost on that
Yawnder
Yawnder•3y ago
No idea. If it works with insomnia, you could inspect using Fiddler and see what is different.
memw
memwOP•3y ago
Oh.
memw
memwOP•3y ago
memw
memwOP•3y ago
User Agent
Yawnder
Yawnder•3y ago
But why does the same credentials work for the same endpoint using another client?
memw
memwOP•3y ago
Because insomnia maybe sends the user agent, i'm not sure what does it send but i'm very sure it does send the user agent
Yawnder
Yawnder•3y ago
Oh! Weird that it's not stated obviously in the doc since it's atypical.
memw
memwOP•3y ago
imma see if all of this user agent thingy works
Yawnder
Yawnder•3y ago
memw
memwOP•3y ago
it was that damn i just
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(Client.Env["GITCREDS"])));
requestClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("JamieWorshipper", "1.0"));
requestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
requestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(Client.Env["GITCREDS"])));
requestClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("JamieWorshipper", "1.0"));
requestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Yawnder
Yawnder•3y ago
Good then!
memw
memwOP•3y ago
thanks a lot and sorry for the missunderstanding before
Yawnder
Yawnder•3y ago
Don't worry. I have a short temper when I'm tired. I should go to bed 😉
Want results from more Discord servers?
Add your server