Start a new topic
Answered

Rest API in C# Login says Invalid Credentials, using Kinvey provided login

I'm trying to login, using the Rest API in C#, but my initial /user/appid/login response says "Unauthorized" and that my credentials are invalid. Here is my code:

string auth = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(appID + ":" + appSecret));
			
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://baas.kinvey.com");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "Basic " + auth);
client.DefaultRequestHeaders.Add("X-Kinvey-API-Version", "3");

HttpResponseMessage userResp = await client.PostAsync("/user/" + appID + "/login", new StringContent("{\"username\": \"" + user + "\",\"password\": \"" + pass + "\"}", Encoding.UTF8, "application/json"));

I've tested this in the console as well, and the Authorization header is the same, as well as the username and password content. The console returns the expected response, but the code doesn't. I have no idea where I'm going wrong here


Many Thanks

Ashley Swanson


Best Answer

 Hi Ashley, I believe the parameters passed into AuthenticationHeaderValue are the scheme (which in this case is basic auth), and the value -- meaning line 5 should be: 

 

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);

 

 

 


Answer

 Hi Ashley, I believe the parameters passed into AuthenticationHeaderValue are the scheme (which in this case is basic auth), and the value -- meaning line 5 should be: 

 

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);

 

 

 

Yep, that did it. Thanks!

Login or Signup to post a comment