I want to be able to download a user from the cloud database for each request and have it available on request in the controller using asp.net mvc. The problem is that the current structure does not support performing asynchronous operations from action filters. Therefore, OnActionExecuting, OnAuthorization methods do not allow me to do this .. for example, I have the following code that DOES NOT work (so don't try). You get an exception: "Asynchronous module or handler completed while asynchronous operation is not yet completed."
protected async override void OnAuthorization(AuthorizationContext filterContext)
{
var user = filterContext.HttpContext.User;
if (!user.Identity.IsAuthenticated)
{
HandleUnauthorizedRequest(filterContext);
return;
}
using (var session = MvcApplication.DocumentStore.OpenAsyncSession())
{
User currentUser = await session.LoadAsync<User>(user.Identity.Name);
if (currentUser == null)
{
HandleUnauthorizedRequest(filterContext);
return;
}
filterContext.HttpContext.Items["User"] = currentUser;
}
}
So is there any other way to do this? I noticed that there is a begin execute method in the base controller:
protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
{
return base.BeginExecute(requestContext, callback, state);
}
Can I do it there, maybe?