1

I would like to authenticate user using Parse Library.

Here are the methods which I would like to make it asyc as api call supports only async call. I am new to MVC and aysc/await functionality. Problem now is that it goes in await method and never returns result and view cant be loaded.

I spend quite some time understanding and trying to use different options but no success yet.

Do I need to use Partialviews or something can be done in ValidateUser method. Any sample code is really appreciated. Thanks.

AccountController.cs

  public ActionResult Login(LoginModel model, string returnUrl)
    {

             if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
                {
                    var loggedIn = true;
                }
        return  View(model);
    }

ParseMembershipProvider : ExtendedMembershipProvider

public override bool ValidateUser(string username, string password)
    {

        var pUserRepo = new PUserRepository();
        bool flag = false;
        var requiredUser = pUserRepo.GetUserObjByUserName(username, password );


        if (requiredUser.Result != null)
            flag = true;

        return flag;

    }

PUserRepository.cs

 public async  Task<ParseUser> GetUserObjByUserName(string userName, string passWord)
{
    return await ParseUser.LogInAsync("test1", "test123");
}
Davin
  • 105
  • 12

2 Answers2

4

You're seeing a deadlock situation due to Result that I explain on my blog.

Unfortunately, membership providers are not (yet) async-aware. So try using ConfigureAwait(false) everywhere in your async methods:

public async  Task<ParseUser> GetUserObjByUserName(string userName, string passWord)
{
  return await ParseUser.LogInAsync("test1", "test123").ConfigureAwait(false);
}

(same for any other async methods you have).

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thanks for your time. Unfortunately I have already tried that and result is always null and task status remains WaitingForActivation. Following thread has detail information that ConfigureAwait(false) should not be used in asp.net. http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code – Davin Mar 25 '13 at 13:42
  • Are you saying that `Result` will return `null` when the task status is `WaitingForActivation`? Because that most certainly should not ever happen. – Stephen Cleary Mar 25 '13 at 13:44
  • @Davin: I'm familiar with that SO question. You can use `ConfigureAwait(false)` as long as you don't need the ASP.NET context in the continuation. – Stephen Cleary Mar 25 '13 at 13:45
  • Result is "The function evaluation requires all threads to run". so I guess still has thread issue. – Davin Mar 25 '13 at 13:47
  • Try breaking the debugger *after* the `Result`. – Stephen Cleary Mar 25 '13 at 14:08
  • It is still blocked on `Result` even after you added `ConfigureAwait(false)` everywhere? – Stephen Cleary Mar 25 '13 at 14:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26877/discussion-between-davin-and-stephen-cleary) – Davin Mar 25 '13 at 15:58
3

Solution is

return Task.Run(() => ParseUser.LogInAsync(userName, passWord)).Result;

So basically I have sync wrapper around async method call. I understood from article that it depends also how that function in Parse library is using await or configureawait(false).

rae1
  • 6,066
  • 4
  • 27
  • 48
Davin
  • 105
  • 12