ASP.NET会话错误重定向 [英] Asp.net session wrong re-direction

查看:96
本文介绍了ASP.NET会话错误重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在下面的代码中,即使我的会话中有"manager",它也会重定向到"index.aspx"


Hi every one,
In the follwing code even If I have "manager" in my session, it is redirecting to "index.aspx"


if (Session["New"] == null || (Session["New"].ToString() != "administrator") || (Session["New"].ToString() != "manager"))
           {
               Response.Redirect("index.aspx");
           }

推荐答案

,因为..

because..

(Session["New"].ToString() != "administrator") is true...



在C#OR ||中条件执行one by one ...

1)将检查空值.[如果为空,则满足条件,将不检查其他条件] [如果不为空,则将检查以下条件]

2)它将检查会话是否为管理员. (如果他不是管理员,则满足条件并重定向到索引页面)

2)如果前两个案例失败,它将跳至第三个(最后一个)案例,并检查会话是否为经理...
(如果他不是经理,则满足条件并重定向到索引页)


解决方案:



in c# OR || conditions executes one by one...

1) it will check for null value.[ if it is null, it satisfies the condition, it wont check for further conditions] [if it is not null, then it will check the following conditions]

2) it will check the session is administrator or not. ( if he is not an administrator it satisfies the condition and redirects to index page)

2) if first two cases fails it will jump to the third ( last ) case and check the session is manager or not...
( if he is not an manager it satisfies the condition and redirects to index page)


Solution:

string newSession = Session["New"] + "";
           string[] values = { "administrator", "manager" };

           if (newSession == "" || !values.Contains(newSession))
               Response.Redirect("index.aspx");


正在重定向,因为您正在使用OR.如果session ["New"] =="manager",则它不等于"administrator",因此满足条件.您可能只需要重新考虑要检查的内容.
It is redirecting because you are using ORs. If session["New"] == "manager" then it does not equal "administrator" and therefore it satisfies the condition. You likely just need to rethink what you are checking for.


这篇关于ASP.NET会话错误重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆