0

I have a service that calls API GET request and return boolean.

Task<bool> LoginExist(string email, string password);

In the controller, I have the code below:

    [HttpPost]
    public ActionResult Login(string email, string password)
    {
        System.Diagnostics.Debug.WriteLine("here");
        bool login_result = _accountService.LoginExist(email, password).Result;
        System.Diagnostics.Debug.WriteLine(login_result);

        if (login_result)
        {
            FormsAuthentication.SetAuthCookie(email, false);
            return Redirect(Request.UrlReferrer.ToString());
        }
        else
        { Redirect("Register"); }

        return Redirect("Register");
    }

However, when I test it, after I click Login, which triggers post request, I can tell GET is successfully executed in my flask api (it returns status 200), however, it never goes to the IF statement or ELSE statement in the code above. Instead, it just kept running...

I wonder if we can use GET within POST and if not, does anyone have better way to approach this?

I added what I have in service:

    public async Task<bool> LoginExist(string email, string password)
    {
        string url = string_url;
        LoginVerification str = await url.WithHeaders(new { Accept = "application /json", User_Agent = "Flurl" }).GetJsonAsync<LoginVerification>();

        return str.login_valid;
    }
Code Dog
  • 3
  • 3
  • A GET is a GET, a POST is a POST. A POST can return data. What is your question? – ProgrammingLlama Dec 07 '18 at 01:12
  • @John So I use GET to call the api which returns a json dictionary, I need to know the value of this json dictionary['login_valid']. And it is treated as a boolean (the result is either true or false). The problem is when I call this boolean in HTTP Post, it does not seem to work. If this is unsolvable, I have no clue how I can do user authentication in controller. – Code Dog Dec 07 '18 at 01:25

1 Answers1

1

The issue here has nothing to do with GET vs. POST. It's how you're using an async method. Accessing the Result property directly is not the proper way to get the result of an async task.

Either change it to call GetAwaiter and GetResult like so:

bool login_result = _accountService.LoginExist(email, password).GetAwaiter().GetResult();

Or better yet, make your action method async and use the await keyword to wait for the result.

[HttpPost]
public async Task<ActionResult> Login(string email, string password)
{
    // ...
    bool login_result = await _accountService.LoginExist(email, password);
    // ...
}

Your intent will be clearer this way and it'll be easier to get things right.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
madreflection
  • 4,744
  • 3
  • 19
  • 29