locked
web api with form authentication and winform client RRS feed

  • Question

  • User264732274 posted

    i need a tutorial which will show me how to pass user credential first time when winform client will call web api function. suppose web api has form authentication then how winform client will pass user credential to web api and read auth cookie but from the next time winclient will not send credential but will send auth cookie to web api just for validation. please redirect me to a valid article which show me how to do it. thanks

    Tuesday, August 2, 2016 10:01 AM

Answers

  • User36583972 posted

    Hi sudip_inn,

    why u use HttpWebRequest class instead of http client class ?

    According to your own choice (which can better achieve)

    in our what i need to pass as domain name argument ?

    Adjust parameters Depending on your environment

    how the url should look like in above ?

    Normal URL

    when i need to pass json as ContentType because web api function will return json.

    request.ContentType = "application/json; charset=utf-8";    
    

    sr.ReadRoEnd() will return json response as string ?

     Depending on your resources return formart.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 9, 2016 9:33 AM

All replies

  • User36583972 posted

    Hi sudip_inn,

    how winform client will pass user credential to web api and read auth cookie but from the next time winclient will not send credential but will send auth cookie to web api just for validation

    May be you can try the following code and test on your winform client.

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");//post url
                // set post headers
                request.Method = "POST";
                request.KeepAlive = true;
                request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                request.UseDefaultCredentials = true;
                request.Credentials = new NetworkCredential("username", "password", "domain");
                request.ContentType = "application/x-www-form-urlencoded";
    
                //CookieContainer cookies = new CookieContainer();
                //string authToken = "api_token";
    
                //// encode to Base64
                //byte[] bytes = Encoding.ASCII.GetBytes(authToken);
                //authToken = Convert.ToBase64String(bytes);
    
                //// write the "Authorization" header
                //request.Headers.Add("Authorization", "Basic " + authToken);
    
                //get data and display the results back onto your website 
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }

    Calling a Web API From a .NET Client in ASP.NET Web API 2 (C#):

    http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Web Service REST Calls With C#:

    https://developer.yahoo.com/dotnet/howto-rest_cs.html

    Best Regards,

    Yohann Lu

    Wednesday, August 3, 2016 4:49 AM
  • User264732274 posted

    why u use HttpWebRequest class instead of http client class ?

    request.Credentials = new NetworkCredential("username", "password", "domain");

    in our what i need to pass as domain name argument ?

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");

    how the url should look like in above ?

    request.ContentType = "application/x-www-form-urlencoded";

    when i need to pass json as ContentType because web api function will return json.

    myResponse = sr.ReadToEnd();

    sr.ReadRoEnd() will return json response as string ?

    Wednesday, August 3, 2016 9:39 AM
  • User36583972 posted

    Hi sudip_inn,

    why u use HttpWebRequest class instead of http client class ?

    According to your own choice (which can better achieve)

    in our what i need to pass as domain name argument ?

    Adjust parameters Depending on your environment

    how the url should look like in above ?

    Normal URL

    when i need to pass json as ContentType because web api function will return json.

    request.ContentType = "application/json; charset=utf-8";    
    

    sr.ReadRoEnd() will return json response as string ?

     Depending on your resources return formart.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 9, 2016 9:33 AM