简体   繁体   English

登录后如何获取/显示用户信息。ASP.net C#

[英]How to get/display the user-information after logging in. ASP.net C#

I am implementing an online voting system for my school-project. 我正在为我的学校项目实施在线投票系统。

After the voter's log-in, i want to display their name, and ID in the label control at the content body. 投票者登录后,我想在内容正文的标签控件中显示其名称和ID。 I try to use SESSION to store the voter's username in the log-in page but I'm not sure of my syntax because nothings happen. 我尝试使用SESSION将选民的用户名存储在登录页面中,但是我不确定我的语法,因为什么也没发生。

I want to know the other way of retrieving a data from database! 我想知道从数据库检索数据的另一种方法! Please teach me. 请教我。

public void GetInformation()
{

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connection;
    cmd.CommandText = "SELECT * FROM tblUsers WHERE voter_name = '" + Session["VotersID"] + "'";
    OleDbDataReader reader = cmd.ExecuteReader();
    if(reader.Read())
    {
        lblVoterName.Text = reader["usr_FirstN"].ToString() + " " + reader["usr_LastN"].ToString();
    }

}

Please Help Me. 请帮我。 Thanks! 谢谢! - -

You can use LogonUserIdentity as follow 您可以按以下方式使用LogonUserIdentity

if (Request.LogonUserIdentity.IsAuthenticated)
   lblName.Text = Request.LogonUserIdentity.Name;

just add this namespace: 只需添加以下名称空间:

using Microsoft.AspNet.Identity;

then you can get LoggedInUserId by: 那么您可以通过以下方式获取LoggedInUserId:

User.Identity.GetUserId();

Or 要么

HttpContext.Current.User.Identity.GetUserId();

So you don't need to use session to keep UserId. 因此,您无需使用会话来保留UserId。

Also you can create Custom Identity and instead of save Username in Name property, storing custom string Store User Data in ASP.NET Identity 您还可以创建自定义身份,而不是在Name属性中保存用户名,而是在ASP.NET Identity中存储自定义字符串“ 存储用户数据”。

get session data and send to one page(register.aspx) to another page(user_home.aspx) 获取会话数据并发送到一个页面(register.aspx)到另一页面(user_home.aspx)

        Session["remail2"] = txtemailsignin.Text;
        Server.Transfer("user_home.aspx", true); 

display the user-information after logging 登录后显示用户信息

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socailweb"].ConnectionString);
        string sql = "select * from tblUsers  where remail='" + Session["remail2"] + "'";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sqldr = cmd.ExecuteReader();
        if (sqldr.Read() == true)
        {

            lblVotersID.Text = sqldr.GetValue(2).ToString();
            lblVoterName.Text = sqldr.GetValue(3).ToString();


        }

        sqldr.Close();
        con.Close();

@Honey Maglangit , what you use is PARAMETER not SESSION. @Honey Maglangit,您使用的是参数而不是会话。

Response.Redirect("VoterPage.aspx?VotersID="+VoterUsername.Text);

So, you should get your VotersID by this way: 因此,您应该通过以下方式获取您的VotersID:

public void GetInformation()
{
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connection;
    cmd.CommandText = "SELECT * FROM tblUsers WHERE voter_name = '" + Request.QueryString["VotersID"].ToString() + "'";
    OleDbDataReader reader = cmd.ExecuteReader();
    if(reader.Read())
    {
        lblVoterName.Text = reader["usr_FirstN"].ToString() + " " + reader["usr_LastN"].ToString();
    }
}

Try it again. 再试一遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM