私はMVC 3アプリケーションを持っています。ユーザーがアプリにログインすると、検証が成功し、セッション変数にユーザー名が保存されると、asp検証が使用されます。
ここでは、すべてが正常に機能します。
次に、AccountController で RedirectionToAction を別のコントローラーに設定します。ここでは、セッション変数が失われます。
**//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });
やってみた
- InProc から ServerState に変更しても問題が解決しません。
- 私のAVを無効にします。
protected 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日が費やされました。どんなアイデアでも、助けは非常に高く評価されます。
同じエラーは、展開または本番で起動します。
この記事セッション変数を失う - セッションが失われる原因となる例外は何ですか?
いくつかの 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; }
}
}
}