Sending Autherization crud authorization - c#

I am writing (Attempting to write a crud application!)
I need to send a Key to the server before I can start using the service.
this.InitializeComponent();
client = new HttpClient();
client.BaseAddress = new Uri("http://dignity-network.org/api/");
I have tried
client.BaseAddress = new Uri("NO3ID81WJECJXZI83A3YYPGJKJLNEJMQ#dignity-network.org/api/")
But this does not work, what is the correct request to send authorization to a server ?
Thanks.

All depends how the server accepts and implements authorization code.
if you have to send the auth code before you start to use , you just need:
HttpContent content = HttpContent.Create(yourAuthCode, Encoding.UTF8);
HttpResponseMessage response = client.Post(new Uri ("http://dignity-network.org/api/"), content);

Related

HttpClient Post Async call

I am trying to call a Windchill Odata rest service. THE HTTP GET method works fine, but it is not working as expected when making a POST request. I am also not sure how to pass the required parameter to the URL. Any suggestions will help a lot.
URL that I am trying to call
http://Hostname/Windchill/servlet/odata/v3/ProdMgmt/Parts('OR:wt.part.WTPart:123456')/PTC.ProdMgmt.GetPartStructure?$expand=Components($select=PartName,PartNumber;$expand=PartUse($select=FindNumber,LineNumber,Quantity,Unit);$levels=1)
Parameter that need to be passed to the URL is ('OR:wt.part.WTPart:123456'). I am doing this in C# .NET.
My C# code
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
var byteArray = Encoding.ASCII.GetBytes("abc:defg!");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Add("CSRF_NONCE", a.NonceValue);
var message = await client.PostAsync("hostname/Windchill/servlet/odata/v3/ProdMgmt/Parts('OR:wt.part.WTPart:123456')/PTC.ProdMgmt.GetPartStructure?$expand=Components($select=PartName,PartNumber;$expand=PartUse($select=FindNumber,LineNumber,Quantity,Unit);$levels=1)", null);
}
Any example or sample code is much appreciated.

Issues getting Amazon Alexa Address API to work

Im having major dramas getting the Amazon Alexa address api to work in the C# Web Api app i have created using AlexaSkillsKit.Net
var apiEndpoint = context.System.ApiEndpoint;
var deviceId = context.System.Device.DeviceId;
var apiAccessToken = context.System.ApiAccessToken;
var url = string.Format("{0}/v1/devices/{1}/settings/address", apiEndpoint, deviceId);
var client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiAccessToken);
client.DefaultRequestHeaders.Add("User-Agent", "Request-Promise");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string response = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
The response i get back is as follows:
{"type":"FORBIDDEN","message":"The authentication token is not valid."}
Im at a loss as to why im getting this error
I have given my app permissions to get the Full Address
I have got this to work, i just needed to send the Permissions Card back to the user
https://developer.amazon.com/docs/custom-skills/device-address-api.html

Error Calling a Odata service in C#

I am trying to call a Odata service from the C# application. I have called the rest services before and consumed the responses in the C#, and trying Odata for the first time. Below is the Code I am using
using (var client = new HttpClient())
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
Uri uri = new Uri(BaseURL);
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = client.GetAsync(uri).Result;
var responsedata = await response.Content.ReadAsStringAsync();
I am using the same URL and the credentials in PostMan and it returns the response. But throws error i the code, is there something different we need to follow calling a Odata services.Please help with this
It is recommended to use a library to access OData. There are at least a couple of libraries that you can choose from, such as:
https://www.nuget.org/packages/Microsoft.OData.Client/ (OData v4)
https://www.nuget.org/packages/Microsoft.Data.OData/ (OData v1..3)

Jasper server rest service returns unauthorized

Using .NET Http client i login to jasper server.
HttpResponseMessage loginResponse = loginClient.PostAsync("http://localhost:8080/jasperserver/rest/login", formContent).Result;
IEnumerable<string> jaspsessid = loginResponse.Headers.GetValues("Set-Cookie");
Using above session id i pass to next request.
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Cookie", jaspsessid);
httpClient.DefaultRequestHeaders.Accept.Add(new
System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
StringContent requestContent = constructJasperRequestJson(reportParameters);
HttpResponseMessage generateReportRequestResponse = new HttpResponseMessage();
generateReportRequestResponse = httpClient.PostAsync(AppConstant.JASPER_SERVER_BASE_URI + AppConstant.JASPER_SERVER_REPORT_EXECUTION_URI, requestContent).Result;
In second request i am getting 401.Unauthorized.
If anyone knows the issue,help me.
You could use a CookieContainer to hold the session cookie, rather than setting a header.
See How do I set a cookie on HttpClient's HttpRequestMessage for an example.

Basic authentication for Silverlight browser

I'm unable to connect to this URL using WebRequestCreator.BrowserHttp, I am however able to connect using WebRequestCreator.ClientHttp. Here's a sample of the code I'm using,
var httpClient = new HttpClient();
WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp);
var byteArray = Encoding.UTF8.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
I'm trying to avoid using the 'Windows Security' dialogue box and I cannot use WebRequestCreator.BrowserHttp for my project.
Edit:
When using WebRequestCreator.BrowserHttp I get
System.ArgumentException: Value does not fall within the expected
range
and nothing in fiddler. If I use WebRequestCreator.ClientHttp I get
Authorization: Basic
in Fiddler
I am able to do both Basic and Bearer (JWT) Authentication on a current silverlight project.
/*
* NOTE:
* It turns out that Silverlight provides HTTP handling in both the Browser and Client stack.
* By default Silverlight uses the Browser for HTTP handling.
* The only problem with this is that Browser HTTP Handling only supports GET and POST request methods.
*/
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
var httpClient = new HttpClient();
var byteArray = Encoding.UTF8.GetBytes("username:password");
// Default headers will be sent with every request sent from this HttpClient instance.
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

Categories

Resources