2012-10-28 197 views
2

我有一个MVC 3应用程序,它使用asp验证 当用户登录应用程序时validadion成功,并将用户名写入会话变量。会话变量丢失mvc

在这里永恒的工作正常。

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

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

我tryed从InProc方式

  1. 更改为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); 
    } 

我有根的项目账户控制器和地区结构的另一个控制器。

Project Structure

几乎10天花在试图解决这个问题。任何想法,帮助将非常令人赞叹。

在部署或生产时会引发相同的错误。

本文Losing my session variables - What exception might cause sessions to be lost?

说一下一些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; } 
    } 
} 
} 
+0

什么是'SessionBag'? –

+0

SessionBag是管理会话变量的对象。我正在发布该代码 –

+2

为什么您将用户名存储在会话中,可以在您不知情的情况下随时处理该会话?一旦你设置了FormsAuthenticationTicket,你就可以从HttpContext.Identity.User.Name呼叫中获得用户名 – Tommy

回答

2

后几天的测试,小时内发现的web.config中的一些问题。

最近我们将我们的应用程序从MVC3更改为MVC4,并发现了网络之间的差异。配置引导结构,我们做了一些修改,删除了一些键和添加其他键。

之后,一切工作正常。

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

+2

那么答案是什么?完成了一些事情,一些工作? –

+2

Jacob。对不起延迟,但有些日子没有阅读stackoverflow。真的我得到一个空的MVC4项目,采取这个web.config并添加我自己的密钥。这解决了我的问题 –

+0

答案是什么?写下来 – mohsen

1

而不是试图调试会话袋约简化这个怎么样。当用户登录时只需设置Sesssion [“test”] = DateTime.Now

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

如果一个新的会话不开始我不相信你的会话正在迷失,并倾向于认为它的实现问题。

当您认为它丢失时,您的会话ID是否会在每个请求中发送?我会猜测它是,因此为什么你看不到一个新的会话被创建。

如果上述测试工作,那么你知道这是一个sessionbag实现问题..

+0

非常感谢您的帮助。但我有点困惑,关于设置Sesssion [“测试”] = DateTime.Now,它意味着将我的sessionid更改为datetimeNow或在会话包中创建测试变量?韩国社交协会。 –

+0

我设置了** Test ** Session [“Test”] = DateTime.Now; 你问我。但在尝试访问其他控制器时也会丢失。 –

+0

在相同的请求或未来的请求或两者?我会在头文件中过来吗?使用glimpse来调试和查看标题或使用fiddler –