Tuesday 15 April 2014

asp.net - Web API Login with Cookie -


I have an ASP.NET Web API and according to the documentation, I need to save an athlete in the cookie, Then pass it back to API requests I can get the authentic token without problems. My question is, what is the best way to save the cookie and send it back in the request.

I make a cookie in RequestMessage, but when requesting the API I can not find any way to send it back.

Update

Now I am able to get the feedback from the feedback I am using this tutorial. Tell me if you want to use this tutorial, make sure you update the code base of web API4. In the method below I'm just trying to login, logout and logout. However, I'm receiving an error code 500

  public HttpWebResponse InitializeWebRequest () {// HttpResponseMessage logoutMessage = wait logout ("bla"). String response data = string Empty; String url = GetServerEndPoint (); String authToken = string.Empty; String login example = "https://example.com"; // Create a request. HttpWebRequest Request = (HttpWebRequest) WebRequest.Create (loginInstance); request. Method = "post"; Request.ContentType = "App / Jason"; Request.CookieContainer = New Cookie Container (); HttpWebResponse response = (HttpWebResponse) request.GetResponseAsync (). result; If (System.IO.StreamReader responseReader = New System.IO.StreamReader (request.GetResponse ()) GetResponseStream ()) by using (response.StatusCode == HttpStatusCode.OK).) {ResponseData = responseReader.ReadToEnd (); } IiLIT & lt; String & gt; AuthHeader = responseData.Split ('{', '}'). ToList (); AuthToken = authHeader [2] .Substring (13, 25); String sessionId = response.Headers.Get (8); Var nv = new NameValueCollection (); NV ["sid"] = sessionId; NV ["token"] = authToken; Cookie header value cookie = value = new cookie header value ("session", nv); // Log out string logInstence 2 = "https://example.com"; HttpWebRequest Request2 = (HttpWebRequest) WebRequest.Create (loginInstance2); Request2 Method = "post"; Request2.ContentType = "App / Jason"; Request2.Headers.Add (NV); HttpWebResponse response2 = (HttpWebResponse) request2.GetResponseAsync () result; } Return response; }    

Wow what a pain!

I do not know why it took me so long to understand me, but after hours and hours and days, to try this stupid proof to work, I finally put it . Here's the code

One weird thing is that I had to create a header format for cookies. Definition that is not a true cookie, it is a very header header value. I had to create the header title because when I removed the JSON object from the file and it changed to the string I was unable to keep the format in the tactic from the file.

  public HttpWebResponse InitiliazeWebRequest () {string response data = string. Empty; String login instance = "url + logincards"; HttpWebRequest Request = (HttpWebRequest) WebRequest.Create (loginInstance); request. Method = "post"; Request.ContentType = "App / Jason"; Request.CookieContainer = New Cookie Container (); HttpWebResponse response = (HttpWebResponse) request.GetResponseAsync (). result; If (System.IO.StreamReader responseReader = New System.IO.StreamReader (request.GetResponse ()) GetResponseStream ()) by using (response.StatusCode == HttpStatusCode.OK).) {ResponseData = responseReader.ReadToEnd (); } Var toke = response.Headers.Get ("authToken"); Jobbject o = Jobbject.Parse (responseData); _authToken = (string) o ["reaction"] ["authToken"]. ToString (); Return feedback; } Return response; } Public HttpWebResponse LogOut () {string responseData = string.Empty; String login example = "https://www.example.com/logout"; HttpWebRequest Request = (HttpWebRequest) WebRequest.Create (loginInstance); request. Method = "received"; Request.ContentType = "App / Jason"; Request.Headers.Add ("cookie: authToken =" + _authToken); HttpWebResponse response = (HttpWebResponse) request.GetResponseAsync (). result; If (System.IO.StreamReader responseReader = New System.IO.StreamReader (request.GetResponse ()) GetResponseStream ()) by using (response.StatusCode == HttpStatusCode.OK).) {ResponseData = responseReader.ReadToEnd (); } Return response; } Return response; }    

No comments:

Post a Comment