我有一个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方式
- 更改为ServerState和问题仍然存在,
- 停用我的AV。
添加保护void Session_Start(){}没有任何反应。会话不会启动或重新启动。
其他许多建议几乎所有与这个相关的主题发表的文章都是由我申请的。
这里是我的代码:
[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);
}
我有根的项目账户控制器和地区结构的另一个控制器。
几乎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; }
}
}
}
什么是'SessionBag'? –
SessionBag是管理会话变量的对象。我正在发布该代码 –
为什么您将用户名存储在会话中,可以在您不知情的情况下随时处理该会话?一旦你设置了FormsAuthenticationTicket,你就可以从HttpContext.Identity.User.Name呼叫中获得用户名 – Tommy