简体   繁体   English

如何在MVC / Membershipprovider中使用异步等待功能

[英]how to use async await functionality with MVC / Membershipprovider

I would like to authenticate user using Parse Library . 我想使用Parse Library对用户进行身份验证。

Here are the methods which I would like to make it asyc as api call supports only async call. 这是我想使其成为asyc的方法,因为api调用仅支持异步调用。 I am new to MVC and aysc/await functionality. 我是MVC和aysc / await功能的新手。 Problem now is that it goes in await method and never returns result and view cant be loaded. 现在的问题是它进入了await方法,从不返回结果并且视图无法加载。

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. 我需要使用Partialviews还是可以在ValidateUser方法中完成某些操作? Any sample code is really appreciated. 任何示例代码都非常感谢。 Thanks. 谢谢。

AccountController.cs 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 ParseMembershipProvider:扩展成员

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 PUserRepository.cs

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

You're seeing a deadlock situation due to Result that I explain on my blog . 由于我在博客上解释了“ Result ,因此您看到了僵局。

Unfortunately, membership providers are not (yet) async -aware. 不幸的是,成员资格提供者尚未意识到async So try using ConfigureAwait(false) everywhere in your async methods: 因此,请尝试在async方法中的所有位置使用ConfigureAwait(false)

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). (与您拥有的其他任何async方法相同)。

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). 我从文章中了解到,这还取决于Parse库中该函数如何使用await或configureawait(false)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM