2

我有一个 MVC 3 应用程序,它在用户登录应用程序时使用 asp 验证,验证成功并在会话变量中保存用户名。

在这里一切正常。

然后在 AccountController 我设置一个 RedirectionToAction,到另一个控制器,这里会话变量丢失。

**//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });

我试过了

  1. 从 InProc 更改为 ServerState 并且问题仍然存在,
  2. 停用我的 AV。
  3. 添加受保护的void Session_Start(){}没有任何反应。会话不会再次启动或重新启动。

  4. 其他许多建议几乎所有与此相关主题发表的文章都被我阅读并应用了。

这是我的代码:

        [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            //  Se inicializa variables para el gestor de mensajes 
            SessionBag.Current.ErrorMessageAuto = new ArrayList();
            SessionBag.Current.ErrorMessage = null;
            SessionBag.Current.ErrorReturnTo = null;

            //Se verifica si el usuario es válido en el contexto de SQL Server, esto para guardar 
             //compatibilidad con el diseño de Merlin para Escritorio.
            Db_Conexion db = new Db_Conexion(model.UserName,model.Password);
            if (!db.connect())
            {
                model.UserName = null;
                model.Password = null;
                SessionBag.Current.ErrorReturnTo = "link";
                SessionBag.Current.ErrorMessage = db.ExceptionsText();
                return View("Mensajes");
            }
            db.close();

            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                SessionBag.Current.UserName = model.UserName;

                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    **//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
                    return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

我在 root 的项目中有帐户控制器,而另一个控制器则由区域构成。

项目结构

花了将近 10 天的时间试图解决这个问题。任何想法,帮助将非常感激。

在部署或生产启动时也会发生同样的错误。

这篇文章丢失我的会话变量 - 什么异常可能导致会话丢失?

说一些 IIS 配置。但并没有准确解释需要配置哪些东西。

SessionBags 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Dynamic;

namespace ParadigmaNet.Infraestructure
{
public sealed class SessionBag : DynamicObject
{
    private static readonly SessionBag sessionBag;

    static SessionBag()
    {
        sessionBag = new SessionBag();
    }

    private SessionBag()
    {
    }

    private HttpSessionStateBase Session
    {
        get { return new HttpSessionStateWrapper(HttpContext.Current.Session); }
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = Session[binder.Name];
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        Session[binder.Name] = value;
        return true;
    }

    public override bool TryGetIndex(GetIndexBinder
           binder, object[] indexes, out object result)
    {
        int index = (int)indexes[0];
        result = Session[index];
        return result != null;
    }

    public override bool TrySetIndex(SetIndexBinder binder,
           object[] indexes, object value)
    {
        int index = (int)indexes[0];
        Session[index] = value;
        return true;
    }

    public static dynamic Current
    {
        get { return sessionBag; }
    }
}
}
4

2 回答 2

1

与其尝试调试会话包,不如简化它。当用户登录时,只需设置 Session["test"]= DateTime.Now

然后直接从每个控制器写出来。

如果新会话没有开始,我不相信您的会话会丢失并且倾向于认为这是一个实施问题。

当您认为会话 ID 丢失时,是否会在每个请求中发送会话 ID?我猜是这样,因此您没有看到创建新会话的原因。

如果上面的测试有效,那么您就知道这是一个 sessionbag 实现问题。

于 2012-10-28T16:54:26.383 回答
1

经过几天和几个小时的测试,在 web.config 上发现了一些问题。

最近我们将我们的应用程序从 MVC3 更改为 MVC4,并发现启动结构上的 web.config 之间存在一些差异,我们进行了一些更改,删除了一些键并添加了其他键。

之后一切正常。

我们生成一个干净的 MVC4 应用程序,然后将 web.config 与旧的 web.config 进行比较,然后删除一些键并修改其他键。

于 2012-10-29T22:24:48.140 回答