我有一个 MVC 3 应用程序,它在用户登录应用程序时使用 asp 验证,验证成功并在会话变量中保存用户名。
在这里一切正常。
然后在 AccountController 我设置一个 RedirectionToAction,到另一个控制器,这里会话变量丢失。
**//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });
我试过了
- 从 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);
}
我在 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; }
}
}
}