C# 获取当前loggedinuserid

C# 获取当前loggedinuserid,c#,asp.net,C#,Asp.net,我想在不使用成员身份的情况下获取用户的当前登录用户ID如何实现 这是我的密码 string CurrentlyLoggedInUser = Session["User"].ToString(); // get the userId for username and compare string userId = CurrentlyLoggedInUser; // assign the userid here and check ok if (Id == user

我想在不使用
成员身份的情况下获取用户的
当前
登录用户ID
如何实现

这是我的密码

 string CurrentlyLoggedInUser = Session["User"].ToString(); // get the userId for username and compare
        string userId = CurrentlyLoggedInUser; // assign the userid here and check ok
        if (Id == userId)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('You cannot delete yourself');window.location ='AdduserProfile.aspx';", true);
            return;
        }
但是我没有得到
ID
。请帮忙

来自会话的值

 if (dt != null && dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["usertype"].ToString() == "0") //SuperAdmin
            {
                Session["UserType"] = "0";
                Session["User"] = dt.Rows[0]["username"].ToString();
                Response.Redirect("csrdashboards.aspx");
            }
            else if (dt.Rows[0]["usertype"].ToString() == "1") // Admin
            {
                Session["UserType"] = "1";
                Session["User"] = dt.Rows[0]["username"].ToString();
                Response.Redirect("csrdashboards.aspx");
            }
            else if (dt.Rows[0]["usertype"].ToString() == "2") // User
            {
                Session["UserType"] = "2";
                Session["User"] = dt.Rows[0]["username"].ToString();
                Response.Redirect("csrdashboards.aspx");
            }
        }
我想在不使用 我怎样才能做到这一点

不要

听着,我知道会员资格并不是API设计中最好的例子。我知道我个人会做一些不同的事情,就像很多其他人一样,而且会有一种强烈的诱惑来推出自己的会员API

但是,如果你对自己的能力没有100%的信心,就不要尝试。安全基础设施太重要,也太容易出错,而且很有可能出错,使您的站点容易受到攻击

我不想让任何人失望,但看看你发布的代码的质量(代码重复、不必要的赋值、多个字典查找、不必要的字符串转换)和你正在做的一些陈述(关于在数据库中有一个“密码”字段,不提哈希),你还没有达到这方面所需的能力水平,你正在走向灾难


“我怎么…?”这是个错误的问题。“我应该…?”是你应该问的第一个问题,我恐怕答案是否定的。使用会员制,它并不完美,但它将比你自己提出的任何东西都更加安全和可靠。

为什么你不能使用会员制?不要在会话中存储用户信息,如果这是
asp.net
,为什么不使用?@DGibbs:明白了,现在是什么解决方案来获取用户
ID
中的当前日志如果你想从会话中加载
ID
,你首先必须将它放在那里!现在,您正在将名为
username
的列的值写入
Session[“User”]
。因此,a)这是一个
Id
吗?b)您的数据源中有任何内容吗?@rahusutar在这种情况下,您的第一段代码毫无意义。如果
Session[“user”]
未持有登录用户的
ID
,为什么要对照
ID
进行检查?为了记录,我知道问题中有说明-我引用了问题。我仍然认为你正走在一条危险的道路上,但你似乎不太可能被说服。