开发者

custom controllerfactory: httpcontext.current.user and httpcontext.current.session are null only on ajax post

I've implemented a custom controller factory in ASP MVC3 with autofac to achieve a very basic plugin management by deriving from DefaultControllerFactory.

In the overrided GetControllerType(RequestContext requestContext, string controllerName) method I do some checks with a session variable or httpcontext.current.user to determine which controller type I must provide to an end user.

The point is that if I do a normal GET (or by using a childaction inside a view) I have those two set when GetControllerType is called but if I do a POST ajax call (with contentType:'application/json; charset=utf-8') via jquery, directly to the actionmethod, the user and the session are null in my httpcontext but they are set an instant later, when I try to access them in my controller action (or in an actionfilter).

Also the AcquireRequestState in this case is never fired and if I navigate through RequestContext property I can see that cookies for session (with session id) are valid.

Do you have any hint?? And why on normal GET calls session and user are set开发者_C百科 at that time and I dont have this problem?

The code is like this:

protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
    if(plugins.All.Where(w=> w.Name == HttpContext.Current.Session["pluginName"].ToString() && w.User == HttpContext.Current.User.Identity.Name) 
        [do stuff]
}

I can avoid Session but I cannot avoid matching for the current user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜