Calling Flipkart Api in asp.net c# - asp.net

I am calling flip kart API in asp.net c# .But i got following error : 401 authorization.
I already included flip kart token and affiliate Id in Rest Header as per Prerequisites and category
URL also.So please tell me why i am getting this error. I am Calling RestApi using HttpClientRequest and my asp.net framework is 4.0

First you need to get product category link list by simple 'Get' using URL
https://affiliate-api.flipkart.net/affiliate/api/<your-affiliate-id>.<json or xml>
the response will have list of various categorizes and their url. These urls require HTTP authentication headers (your affiliate-id and access token) in the GET request. You can add like :-
WebClient wc = new WebClient();
wc.Headers.Add("Fk-Affiliate-Id", "blueskyvic");
wc.Headers.Add("Fk-Affiliate-Token", "your access code");
string res = wc.DownloadString("https://affiliate-api.flipkart.net/affiliate/feeds/blueskyvic/category/v1:6bo-ul6.json?expiresAt=1420832915393&sig=6c1167c0d141bd3edab28f9f2a980a30");//tv_video_accessories
The response will have nexturl property which will give you listing of next 50 items in that category.

Related

Authentication prevents posting to Classic ASP from Web form using WebClient() from Code Behind

I am managing and old web site (site, not application) that is a hybrid of Web Forms and Classic ASP. The Classic ASP is being phased out, but that is probably a year away. Right now, we are dropping the old form of authentication in favor of Windows Authentication in the web.config.
The problem is that I am attempting to post to a Classic page from the code behind of a web form (http://www.blahsiblah.com/index.aspx) and am getting a 401 error.
var webClient = new WebClient();
var urlClassicASP = "http://www.blahsiblah.com/classic.asp";
var responseArray = webClient.UploadValues(urlClassicASP, "POST", nameValueCollection);
This throws "The remote server returned an error: (401) Unauthorized"
My question is, how can I post to the classic page without invoking the authentication of the dotNet side?
There are multiple ways to achieve this
Here is a simple suggestion that I hope helps
.Net
Use 127.0.0.1 (or your internal 192.169 / 10.1* ) IP to post to the page vs the public URL
Add a parameter (call it 'bypassauth' or something unique ) when sending the request to the ASP page
Add a parameter that identifies the user that you have authenticated in the .Net side
ASP
Find the include where the authentication check is happening and in that check, add another condition before returning 401 that checks two things
1) Request is from local/internal IP
2) Has the bypassauth parameter
3) the user id is valid
This way your old ASP code will still continue to work if requested from a browser and expect user to be authenticated however, when sending the request from .net will let you bypass authentication
I'm sure there are other ideas too, but this is just one approach
My solution was to set:
webClient.UseDefaultCredentials = true;

Using reserved word "action" as an MVC parameter name

To provide details regarding my platform. I am using Visual Studio 2017 with a .NET Core Web project.
I am implementing a new back-end for a client for which I cannot alter the client. I am attempting to use MVC to accomplish this.
The URL which I must be able to respond to is -> https://my.server.com:8443/portal/gateway?sessionId=0A6405100000001D04FB55FE&portal=4ba457d0-e371-11e6-92ce-005056873bd0&action=cwa&token=3fcbc246bb3c35a5fa8055fec6bbf431
I would like to extract the values for :
sessionId
portal
action
token
As they all have meaning.
I have created a new view folder within my project called "Portal" and have created a new MVC controller named "PortalController".
I have created a new View called gateway.cshtml and also have implemented the following function ->
public IActionResult Gateway(string sessionId, string portal, string action, string token)
{
ViewData["Message"] = "Gateway";
return View();
}
When I set a breakpoint within the function, I receive the following values for the URL provided above ->
sessionId "0A6405100000001D04FB55FE" string
portal "4ba457d0-e371-11e6-92ce-005056873bd0" string
action "gateway" string
token "3fcbc246bb3c35a5fa8055fec6bbf431" string
As can be seen in the output of the debugger, for the parameter named "action", isntead of receiving the value "cwa" as passed by the client, I instead receive the name "gateway" which is passed by MVC as the name of the action.
Is there a "proper way" of handling this?
I would prefer to avoid altering the routes within the startup.cs file as this hurts the modularity of the code. I would welcome attributes that could be used to reroute the parameter names.
Another alternative I could see (buy can't figure out) is to simply make use of the HTTP request to read the values I'm interested in instead of using function parameters.
Thank you very much in advance for any assistance you can provide!
In .Net Core you can get your action parameter value straight off the Request with
var action= Request.Query["action"];
where in .Net Framework it was
var action= Request.QueryString["action"];

asp.net mvc soap request

i have URL like this : https://soapserver.example.com/blahblah.asmx?wsdl
i need to send some params to the this URL and get response.
1 - i created asp.net mvc project
2- Generated Service reference - Project > Add Service Reference > Submited URL
3 - edited controller like this
PromoKontrol.ServiceSoapClient ProKontrol = new PromoKontrol.ServiceSoapClient();
ProKontrol.PromosyonKoduKontrol(UserName, Password);
i am using breakpoint but i cant see any response?
Please make sure you assign the data returning from the service to some variable.
In the below code keep the debugging point and check the returnDate;
PromoKontrol.ServiceSoapClient ProKontrol = new PromoKontrol.ServiceSoapClient();
var returnDate = ProKontrol.PromosyonKoduKontrol(UserName, Password);

BufferedMediaTypeFormatter HttpContent does not contain all Headers sent in Request

Within my Owin Self hosted Web Api project I am trying to build a custom MediaTypeFormatter that inherits from BufferedMediaTypeFormatter.
But the problem is the HttpContent object passed into ReadFromStream(..) does not contain all the headers sent in the request.
How do you access ALL the headers that were sent in the request (I know this because I made the request), or access the original HttpRequestMessage in the ReadFromStream(..) method ?
This seams to be a major bug and I cannot think of any reason why all the Request headers are not provided.
Sounds like the ASP.NET Request object does not expose a property for the specific header field name you are looking for.
So it looks like first, you need the Request and to do that you might be able to override GetPerRequestFormatterInstance:
How do I retrieve the HTTP request method in an ASP.NET WebAPI MediaTypeFormatter?
Once you have the request object you can search for the specific header field name you are looking for like so:
IEnumerable<string> headerValues = request.Headers.GetValues("MyHeaderFieldName");
var id = headerValues.FirstOrDefault();
You can also get the raw request if needed:
Request.InputStream.Position = 0;
var input = new StreamReader(Request.InputStream).ReadToEnd();

How Do I Get RouteData Values from a Web Service in .Net 4.0

I am trying to extract an id number from a URL using a web service so that it can be used as a parameter for a where clause in a select statement that produces data from a database based on the id number of a record. That data will then be passed back to the page to populate an element in a jQuery modal popup widow.
Everything works fine with a static id number (ex: string postid = "120"), but I don't know how to get the id number from the URL. I'm using Routing in .Net 4 and the method for accessing Routing in pages does not work in a web service. In pages I just do stuff like var id = RouteData.Values["id"]; and that gets the id, but when i did it in a web service I got an error:
CS0120: An object reference is required for the non-static field,
method, or property 'System.Web.Routing.RouteData.Values.get'
Summary:
I have web service accessed form a details page where I want to get RouteData for the page making the request. I want to do this just as easily as I can on a page using RouteData.Values which is just as easy as the now the obsolete Request.Querystring.
Now I am more confused because although I could easily add a new route for the web service I don't know I would call that using jQuery Ajax because of the webservice.asmx/webmethod syntax.
Right now I have URL: "../webservices/googlemaps.asmx/GetGoogleMap" in my jQuery Ajax, but that is not a real URL. It only exists in jQuery somewhere and the way to call the service using just JavaScript is no a real URL either, its webservice.webmethod() which in this case would be googlemaps.GetGoogleMap().
I will try registering a route for webservices/googlemaps.asmx/GetGoogleMap/postid, but I doubt it will work because GetGoogleMap is not a directory or a querystring.
Get current http request and use RequestContext property to get request context - it has current routing data. For example,
var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];
In case of WCF based web service, make sure that service is participating in ASP.NET pipeline (see ASP.NET Compatibility)
EDIT: Sorry for misleading answer - the above will not work unless web service url is registered in routing engine. However, it may not solve your issue of retrieving the id - what kind of service implementation are you using? Are you making a GET request or POST request? Typically, web service handler (asmx) or WCF pipeline should convert GET/POST parameters to method parameters. Post your web service code and how you invoke it.

Resources