没有绑定到当前上下文会话时重定向到登录页面 [英] No session bound to the current context when redirecting to login page

查看:146
本文介绍了没有绑定到当前上下文会话时重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题。我已经成立了,并随后在 ASP.NET MVC4指令和由preSS 公布在Web API MVC 4应用uisng NHibernate的。

I have encounter a strange problem. I've set up and MVC 4 app uisng NHibernate followed by instructions in ASP.NET MVC4 and the Web API published by apress.

我用命名的控制器属性行为管理会话 [LoggingNhibernateSessionAttribute] 。它运作良好,我可以与NHibernate做CRUD操作。

I manage session for actions using controller attribute named [LoggingNhibernateSessionAttribute]. It works well and I can do CRUD operations with NHibernate.

但出现问题时,我添加批准属性的HomeController其重定向到登录页面。此刻发生异常时,指示

But problem arise when I add an authorize attribute to HomeController which redirects to login page. at the moment an exception occurs indicating

不会话绑定到当前上下文

"No session bound to the current context"

我看了很多我的code。但我不明白为什么会不绑定到当前上下文。

I looked a lot at my code. but I couldn't understand why session doesn't bound to current context.

public class LoggingNHibernateSessionAttribute : ActionFilterAttribute
{
    private readonly IActionLogHelper _actionLogHelper;
    private readonly IActionExceptionHandler _actionExceptionHandler;
    private readonly IActionTransactionHelper _actionTransactionHelper;

    public LoggingNHibernateSessionAttribute()
        : this(WebContainerManager.Get<IActionLogHelper>(),
        WebContainerManager.Get<IActionExceptionHandler>(),
        WebContainerManager.Get<IActionTransactionHelper>())
    {
    }

    public LoggingNHibernateSessionAttribute(
        IActionLogHelper actionLogHelper,
        IActionExceptionHandler actionExceptionHandler,
        IActionTransactionHelper actionTransactionHelper)
    {
        _actionLogHelper = actionLogHelper;
        _actionExceptionHandler = actionExceptionHandler;
        _actionTransactionHelper = actionTransactionHelper;
    }

    public override void OnActionExecuting(ActionExecutingContext actionExectingContext)
    {
        _actionLogHelper.LogEntry(actionExectingContext.ActionDescriptor);
        _actionTransactionHelper.BeginTransaction();
    }

    public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
    {
        _actionTransactionHelper.EndTransaction(actionExecutedContext);
        _actionTransactionHelper.CloseSession();
        _actionExceptionHandler.HandleException(actionExecutedContext);
        _actionLogHelper.LogExit(actionExecutedContext.ActionDescriptor);
    }
}

ActionTransactionHelper

public ActionTransactionHelper(
        ISessionFactory sessionFactory,
        ICurrentSessionContextAdapter currentSessionContextAdapter)
    {
        _sessionFactory = sessionFactory;
        _currentSessionContextAdapter = currentSessionContextAdapter;
    }

    public void BeginTransaction()
    {
        var session = _sessionFactory.GetCurrentSession();
        if (session != null)
        {
            session.BeginTransaction();
        }
    }

在这里我如何绑定会话当前SessionContext被:

and here how I bind session to current sessionContext:

private ISession CreateSession(IContext context)
    {
        var sessionFactory = context.Kernel.Get<ISessionFactory>();
        if (!CurrentSessionContext.HasBind(sessionFactory))
        {
            // Open new ISession and bind it to the current session context
            var session = sessionFactory.OpenSession();
            CurrentSessionContext.Bind(session);
        }
        return sessionFactory.GetCurrentSession();
    }

没有任何人有任何想法?

does anyone have any idea?

推荐答案

这里的问题是有关的IFilter 的执行顺序属性,描述如下:<一href=\"http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs\"相对= 理解的行为过滤器>(下面举)的

The issue here is related to the execution order of IFilter Attributes, described here: Understanding Action Filters (cite below)

ASP.NET MVC框架支持四种不同类型的过滤器:

The ASP.NET MVC framework supports four different types of filters:


      
  1. 授权过滤器 - 实现了个IAuthorizationFilter属性

  2.   
  3. 动作过滤器 - 实现IActionFilter属性

  4.   
  5. 结果过滤器 - 实现IResultFilter属性

  6.   
  7. 异常过滤器 - 实现了个IExceptionFilter属性

  8.   
  1. Authorization filters – Implements the IAuthorizationFilter attribute.
  2. Action filters – Implements the IActionFilter attribute.
  3. Result filters – Implements the IResultFilter attribute.
  4. Exception filters – Implements the IExceptionFilter attribute.

过滤器上面所列的顺序执行。例如,授权过滤器总是在行动过滤器和过滤器例外所有其他类型的过滤器后,总是在执行之前执行。

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.

所以,我们exeperience的是:

So, what we exeperience is:


  • 在行动过滤器(第二个),我们开始做NHibernate的会议

  • 在授权过滤器(非常第一个),我们预计会议要到位

一个解决方法是在 OnAuthorize 事件启动会话...

One solution is to start session in the OnAuthorize event...

这篇关于没有绑定到当前上下文会话时重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆