2013-11-05 22 views
0

我被告知要爲管理員建立一個網站來爲用戶創建調查問卷。 註冊後,管理員/用戶將獲得唯一的用戶ID。 (我已經設置了用戶名「身份規範」爲「是」,所以它會自動遞增。)如何捕獲當前登錄的管理員用戶標識並將其顯示在另一頁上?

登錄到管理員帳戶後....他們將被引導到CreateSurvey頁那裏的標籤和文本框在點擊提交按鈕後輸入以下('SurveyID','SurveyName','CreatedBy'和'DateCreated')到數據庫中。

我需要有管理員的用戶名一套「CreatedBy」使管理員不必輸入自己的用戶名

我如何捕獲當前登錄的管理員用戶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; 
    } 
} 

回答

0

您可以用於存儲用戶名使用會話。

// 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"); 
+0

對不起,我是新手。我應該在哪裏舉行會議? – Yinghui

+0

在您的CreateSurvey頁面中,您可以設置'txtBoxCreatedBy.Text =(string)(Session [「name」]);' – pblyt

0

您是如何驗證管理員的呢?

如果您使用的是Windows或表單認證,你應該能夠使用:

User.Identity.Name 

來獲取當前用戶名。

+0

我使用布爾值來檢查用戶的角色是否爲admin。我已經更新了我的問題,你能幫助我通過它並找到我的錯誤嗎? :( – Yinghui

相關問題