0

I am implementing RESTful services(asp.net web api 2.0) to perform authentication in Webforms.From my client end(Webforms) i am generating a token and sending it for validation in serverside(Webapi controller).

Webform codeBehind file:

static async Task RunAsync(string token)
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.UseDefaultCredentials = true;
        //HttpClient Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
        using (var client = new HttpClient(handler))
        {
            client.BaseAddress = new Uri("http://localhost:32253/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Authorization-Token", token);
            string uri = "api/UserDetail/Authenticate";

            //The GetAsync method sends the HTTP GET request; The await keyword suspends execution until the asynchronous method completes
            HttpResponseMessage response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                //Use ReadAsAsync to deserialize the JSON. The ReadAsync method is asynchronous because the response body can be arbitrarily large.
                ValidateUserStatus success = await response.Content.ReadAsAsync<ValidateUserStatus>();
                result = "Success";
            }
        }

Serverside Method:

    [System.Web.Http.AcceptVerbs("GET")]
    [System.Web.Http.HttpGet]
    [ActionName("Authenticate")]

    public SLS.MDS.Web.Entities.Enums.ValidateUserStatus Validate()
    {
        var token = Request.Headers.GetValues("Authorization-Token").First();
        //string decryptedData = RSAClass.Decrypt(token);
        var comparetoken = HMACSHA256Class.Hash(username, password, data);
        string[] credentials = data.Split(',');
        SLS.MDS.Web.Entities.Enums.ValidateUserStatus status = SLS.MDS.Web.Entities.Enums.ValidateUserStatus.NotSet;
        if (token == comparetoken)
        {
            if (credentials != null)
            {
                status = MDSProvider.DefaultInstance.ValidateIDSUser(credentials[0], credentials[1]);
            }
        }

        return status;
    }

The validation in serverside is successful and returns status.The issue i am facing is that i am unable to receive it in client end(Webform). In line ,

HttpResponseMessage response = await client.GetAsync(uri);

The httprequest goes to serverside and returns status but not able to receive it.What would be the reason ? Can someone help me out?

kaarthick raman
  • 793
  • 2
  • 13
  • 41

1 Answers1

1

This question comes up quite often on Stack Overflow. I suggest having a look at this answer to get a good understanding of what's happening: https://stackoverflow.com/a/10351400/3299157

Basically you are running into a deadlock situation because of the nature of these async requests.

Community
  • 1
  • 1
Ageonix
  • 1,748
  • 2
  • 19
  • 32