Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何捕获当前登录的管理员的用户ID并将其显示在另一个页面上?_C#_Database_Visual Studio 2010_Textbox - Fatal编程技术网

C# 如何捕获当前登录的管理员的用户ID并将其显示在另一个页面上?

C# 如何捕获当前登录的管理员的用户ID并将其显示在另一个页面上?,c#,database,visual-studio-2010,textbox,C#,Database,Visual Studio 2010,Textbox,我被告知为管理员建立一个网站,为用户创建调查。 注册后,管理员/用户将获得唯一的用户ID。我已经将UserID“Identity Specification”设置为“Yes”,因此它将自动递增 登录到管理员帐户后。。。。他们将被定向到CreateSurvey页面,其中有标签和文本框,可在单击submit按钮后将以下“SurveyID”、“SurveyName”、“CreatedBy”和“DateCreated”输入数据库 我需要将管理员的UserID设置为“CreatedBy”,这样管理员就不必

我被告知为管理员建立一个网站,为用户创建调查。 注册后,管理员/用户将获得唯一的用户ID。我已经将UserID“Identity Specification”设置为“Yes”,因此它将自动递增

登录到管理员帐户后。。。。他们将被定向到CreateSurvey页面,其中有标签和文本框,可在单击submit按钮后将以下“SurveyID”、“SurveyName”、“CreatedBy”和“DateCreated”输入数据库

我需要将管理员的UserID设置为“CreatedBy”,这样管理员就不必输入他们的UserID

如何捕获当前登录的管理员的用户ID以将其设置为“CreatedBy”

登录页面:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Session["name"] = txtBoxName.Text;
    Session["password"] = txtBoxPassword.Text;

    string name = txtBoxName.Text;
    string password = txtBoxPassword.Text;
    string admin = "";

    Boolean check = checkuser(name, password, ref admin);

    if (check == true)
    {
        if (admin.ToLower() == "admin")
        {
            string url = string.Format("~/Admin/Admin.aspx?name={0}", txtBoxName.Text);
            Response.Redirect(url);
        }
        else
        {
            string url = string.Format("~/User/SurveyWorks.aspx?name={0}", txtBoxName.Text);
            Response.Redirect(url);
        }
    }
    else
    {
        ShowAlert("Please try again!");
    }
}

public Boolean checkuser(string name, string password, ref string checkAdmin)
{
    Boolean check = false;
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString =
    @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SurveyFdBk_DB.mdf;Integrated Security=True;User Instance=True";


    var comd = connection.CreateCommand();
    try
    {
        connection.Open();
        comd.CommandText = "SELECT UserID, Name, Role FROM Users WHERE Name = '" + name + "' and Password = '" + password + "'";
        SqlDataReader dr = comd.ExecuteReader();

        if (dr.Read())
        {
            checkAdmin = dr["Role"].ToString();
            Session["UserID"] = dr["UserID"].ToString();
            Session["Name"] = dr["Name"].ToString();
            check = true;
        }

        else
        {
            check = false;
        }
    }
    finally
    {
        connection.Close();
    }

    return check;
}
注册页面:

protected void btnSubmitRegistration_Click(object sender, EventArgs e)
{
    SqlConnection connection = null;
    SqlCommand command = null;

    try
    {
        string connectionString = ConfigurationManager.ConnectionStrings["SurveyFdDBConnString"].ConnectionString;
        connection = new SqlConnection(connectionString);
        connection.Open();
        string type = lblMsg.Text;

        string sql = "Insert into Users (Name, Company, Password, Role, DateCreated) Values " + "(@Name, @Company, @Password, @Role, @DateCreated)";
        command = new SqlCommand(sql, connection);
        command.Parameters.AddWithValue("@Name", txtBoxName.Text);
        command.Parameters.AddWithValue("@Company", txtBoxCompany.Text);
        command.Parameters.AddWithValue("@Role", txtBoxRole.Text);
        command.Parameters.AddWithValue("@Password", txtBoxPassword.Text);
        command.Parameters.AddWithValue("@DateCreated", DateTime.Now);

        if (!string.IsNullOrEmpty(txtBoxName.Text))
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
            @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SurveyFdBk_DB.mdf;Integrated Security=True;User Instance=True";

            conn.Open();
            SqlCommand cmd = new SqlCommand("select Name from Users where Name= @Name", conn);
            cmd.Parameters.AddWithValue("@Name", txtBoxName.Text);
            SqlDataReader dr = cmd.ExecuteReader();

            int rowCount = command.ExecuteNonQuery();

            if (dr.HasRows)
            {
                ShowAlert("Username Taken");
            }

            else if (rowCount != 0)
            {
                Response.Write("Registration Success.<br/>");
            }

            conn.Close();
        }
    }


    catch (Exception ex)
    {
        Response.Write("Error: " + ex.Message);
    }


    finally
    {
        if (connection != null)
            connection.Close();

        txtBoxName.Text = string.Empty;
        txtBoxCompany.Text = string.Empty;
    }
}
CreateSurvey页面:

protected void btnCreateSurvey_Click(object sender, EventArgs e)
{
    SqlConnection connection = null;
    SqlCommand command = null;

    try
    {
        string connectionString = ConfigurationManager.ConnectionStrings["SurveyFdDBConnString"].ConnectionString;
        connection = new SqlConnection(connectionString);
        connection.Open();

        string sql = "Insert into Survey (SurveyID, SurveyName, CreatedBy, DateCreated, Anonymous) Values " + "(@SurveyID, @SurveyName, @CreatedBy, @DateCreated, @Anonymous)";
        command = new SqlCommand(sql, connection);
        command.Parameters.AddWithValue("@SurveyID", txtBoxSurveyID.Text);
        command.Parameters.AddWithValue("@SurveyName", txtBoxSurveyName.Text);
        command.Parameters.AddWithValue("@CreatedBy", txtBoxCreatedBy.Text);
        command.Parameters.AddWithValue("@DateCreated", DateTime.Now);
        command.Parameters.AddWithValue("@Anonymous", txtBoxAnonymous.Text);

        int rowCount = command.ExecuteNonQuery();

        if (rowCount != 0)
        {
            Response.Write("Survey created successfully.<br/>");
            Response.Redirect("~/Admin/SetSurveyQuestions.aspx");
        }
    }

    catch (Exception ex)
    {
        Response.Write("Error: " + ex.Message);
    }

    finally
    {
        connection.Close();
        txtBoxSurveyID.Text = string.Empty;
        txtBoxSurveyName.Text = string.Empty;
        txtBoxAnonymous.Text = string.Empty;
    }
}

您可以使用会话来存储用户标识

// To save UserID in session
Session.Add("userID", "123");
// or
Session["userID"] = "123";

// Get UserID from session
string userID = (string)(Session["userID"]);

// Remove from session
Session.Remove("userID");

你首先是如何认证管理员的

如果您使用的是Windows或Forms身份验证,您应该能够使用:

User.Identity.Name

获取当前用户名。

我使用布尔值检查用户的角色是否为admin。我已经更新了我的问题,你能帮我检查一下并找出我的错误吗对不起,我是新手。会话应该放在哪里?在CreateSurvey页面中,可以设置txtBoxCreatedBy.Text=stringSession[name];