会话变量值进行比较,以一个字符串 [英] comparing session variable value to a string

查看:135
本文介绍了会话变量值进行比较,以一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会话变量比较字符串以检查登录类型管理员或不是。

I am comparing the session variable to a string to check if the login type is administrator or not.

code,我使用:

if (Session["loggedInUsername"] == null)
        {
            btnLogin.Text = "Sign In";
            lblWelcome.Text = "Welcome!";
            hypManageRestaurants.Enabled = false;
            hypManageReviews.Enabled = false;
            hypPostReviews.Enabled = false;

        }
        else
        {
            if (Session["loggedInUserType"] == "Administrator")
            {
                hypManageRestaurants.Enabled = true;
                hypManageReviews.Enabled = true;
                hypPostReviews.Enabled = true;
            }
            else
            {
                hypManageRestaurants.Enabled = false;
                hypManageReviews.Enabled = false;
                hypPostReviews.Enabled = true;
            }
            lblWelcome.Text = "Welcome " + Session["loggedInUsername"];

            btnLogin.Text = "Sign Out";
        }

所以,首先我检查是否有用户已经登录或没有。如果用户成功登录,会话变量loggedInUsername将用户名的值。如果loggedInUsername会话变量是不是空的,它会检查loggedInUserType会话变量为登录用户的类型。

So first I am checking if any user has logged in or not. If the user logs in successfully, the session variable "loggedInUsername" will have the value of the username. If the "loggedInUsername" session variable is not empty, it will check "loggedInUserType" session variable for the type of the logged in user.

下面来了奇怪的事情,在loggedInUserType的价值是完全管理员不,在那里我会话变量比较字符串IF函数管理员正在被跳过,转到别的。

Here comes the weird thing, the value of the "loggedInUserType" is exactly "Administrator" without the "", at the if function where I am comparing the session variable to the string "Administrator" is being skipped and goes to the else.

所有会话变量越来越值时,用户登录

All session variables are getting values when the user logs in.

下面是我使用的登录数据和这个纪录是有型的管理员的唯一记录。

Below is the data which I am using to login and this record is the only record which have a type of "Administrator".

链接图片

有没有其他的方法来一个会话变量比较字符串

Is there any other method to compare a session variable to a string

推荐答案

对象类型值转换为字符串

Cast the object type value to a string

((string)Session["loggedInUserType"]) == "Administrator"

这篇关于会话变量值进行比较,以一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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