C# 会话变量在操作筛选器中变为null

C# 会话变量在操作筛选器中变为null,c#,asp.net,asp.net-mvc,session,session-variables,C#,Asp.net,Asp.net Mvc,Session,Session Variables,我正在尝试创建一个操作筛选器,以便在用户有未确认的电子邮件地址时将用户重定向到ConfirmEmail操作。在我的控制器中设置会话变量,在我的操作过滤器中检查会话变量是否为null public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { // some code here if (!await UserManager.Is

我正在尝试创建一个操作筛选器,以便在用户有未确认的电子邮件地址时将用户重定向到
ConfirmEmail
操作。在我的控制器中设置会话变量,在我的操作过滤器中检查会话变量是否为null

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    { 
          // some code here

          if (!await UserManager.IsEmailConfirmedAsync(user.Id))
          {
            Session["ConfirmEmail"] = "true";
          }

         // some code here
     }

    public class ConfirmEmailFilter : ActionFilterAttribute
    {    
          public override void OnActionExecuting(ActionExecutingContext filterContext)
                {
                    if (filterContext.HttpContext.Session["ConfirmEmail"] != null)
                    {
                       if (!(bool)filterContext.HttpContext.Session["ConfirmEmail"])
                       {
                        filterContext.Result = new RedirectToRouteResult(
                            new RouteValueDictionary
                            {
                                {"controller", "Account"},
                                {"action", "ConfirmEmail"}
                            });
                        }
                    }
                 }
      }
公共异步任务登录(LoginViewModel模型,字符串返回URL)
{ 
//这里有一些代码
如果(!wait UserManager.IsEmailConfirmedAsync(user.Id))
{
会话[“确认邮件”]=“真”;
}
//这里有一些代码
}
公共类ConfirmMailFilter:ActionFilterAttribute
{    
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
if(filterContext.HttpContext.Session[“ConfirmEmail”]!=null)
{
if(!(bool)filterContext.HttpContext.Session[“ConfirmEmail”])
{
filterContext.Result=新的RedirectToRouteResult(
新RouteValueDictionary
{
{“控制器”、“帐户”},
{“操作”、“确认邮件”}
});
}
}
}
}
我不确定操作筛选器无法读取会话的原因。我是否应该使用其他方法来执行此检查

编辑

我发现了问题!在我的代码中(在设置会话之前,我有以下内容)
Session.Clear()
会话。放弃()。我不认为这会是一个问题,因为它是在我设置会话之前调用的,但我想它是

我认为您的逻辑有点不正确

  • 尝试传递布尔值,而不是字符串值“true”


  • 希望这能有所帮助。

    我觉得你的逻辑有点错误

  • 尝试传递布尔值,而不是字符串值“true”


  • 希望这能有所帮助。

    作为一种暗箭伤人的做法,将if语句更改为:
    if(!await UserManager.IsEmailConfirmedAsync(user.Id.ConfigureAwait(false))
    行得通吗?我知道if语句不是问题,因为在调试时,我看到它进入了if语句和
    会话[“ConfirmEmail”]
    实际上等于true。但是,一旦它进入我的filter类,
    filterContext.HttpContext.Session[“ConfirmEmail”]
    就被删除了false@EitanK-是
    false
    还是
    “false”
    ?您将session变量设置为string
    “true”
    ,但希望类型转换
    (bool)session[…]
    能够工作-它不会工作。您需要将变量设置为布尔值
    true
    ,或者使用
    System.Convert.ToBoolean(…)
    @Igor我修复了该部分,谢谢。但是,由于操作筛选器认为
    Session[“confirmeai”]
    为空,因此它永远不会到达这一点。您是否尝试过将某个内容放入控制器的会话中,然后在操作筛选器中读取它,而不使用任何逻辑(无if或cast)?可能您必须在控制器上放置
    [SessionState(SessionStateBehavior.Required)]
    如果将if语句更改为:
    if(!wait UserManager.IsEmailConfirmedAsync(user.Id).configureWait(false)),这是暗中的一个疯狂尝试
    work?我知道我的if语句不是问题,因为当我调试时,我看到它包含在if语句中,
    Session[“ConfirmEmail”]
    实际上等于true。但是,一旦它进入我的filter类,
    filterContext.HttpContext.Session[“ConfirmEmail”]
    就被删除了false@EitanK-是
    false
    还是
    “false”
    ?您将session变量设置为string
    “true”
    ,但希望类型转换
    (bool)session[…]
    能够工作-它不会工作。您需要将变量设置为布尔值
    true
    ,或者使用
    System.Convert.ToBoolean(…)
    @Igor我修复了该部分,谢谢。但是,由于操作筛选器认为
    Session[“confirmeai”]
    为空,因此它永远不会到达这一点。您是否尝试过将某个内容放入控制器的会话中,然后在操作筛选器中读取它,而不使用任何逻辑(无if或cast)?可能您必须将
    [SessionState(SessionStateBehavior.Required)]
    放在您的控制器上。问题是,无论传递的值是字符串还是布尔值,在调试器到达我的筛选器类时都会变为null。因此,它甚至没有进入
    OnActionExecuting
    方法中的第二个if语句。此外,当我将访问权限更改为受保护时,我会收到以下错误消息:
    在重写'public'继承成员'System.Web.Mvc.ActionFilterAttribute.OnActionExecuting时无法更改访问修饰符。问题是,无论传递的值是字符串还是bool,在调试器访问我的筛选器时都会变为null班级。因此,它甚至没有进入
    OnActionExecuting
    方法中的第二个if语句。此外,当我更改对受保护成员的访问权限时,我会收到以下错误消息:
    在重写'public'继承成员'System.Web.Mvc.ActionFilterAttribute.OnActionExecuting
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!await UserManager<>.IsEmailConfirmedAsync(user.Id))
            {
                Session["ConfirmEmail"] = true;
                RedirectToAction("ConfirmEmail");
            }
        }
    
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Session["ConfirmEmail"] != null)
        {
            if (filterContext.HttpContext.Session["ConfirmEmail"] is bool && (bool) filterContext.HttpContext.Session["ConfirmEmail"])
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        {"controller", "Account"},
                        {"action", "ConfirmEmail"}
                    });
            }
        }
    }