2011-05-05 43 views
0

我有一個腳本在我的site.master頁面更新的SQL,它如下圖所示,但不是更新Test我想更新誰剛登錄的用戶的正常工作。獲取OnLoggedIn用戶名和更新用戶在SQL

如何選擇當前用戶?

我發現以下,但不知道這是否是正確的,它應該被添加:

System.Web.HttpContext.Current.User.Identity.Name 

我使用窗體身份驗證。

<script runat="server"> 
void OnLoggedIn(object sender, EventArgs e) 
{ 
    //connect to the db 
    SqlConnection conn = new SqlConnection(WebConfigurationManager. 
     ConnectionStrings["herning_brand_dk_dbConnectionString"].ConnectionString); 
    //the command to increment the value in the LoginCounter column by 1 
    SqlCommand cmd = new SqlCommand("UPDATE aspnet_Users SET 
     LoginCounter = LoginCounter+1 WHERE UserName = 'Test'", conn); 
    cmd.CommandType = CommandType.Text; 
    //update where UserName is Test 
    cmd.Parameters.AddWithValue("UserName", "Test"); 
    using (conn) 
    { 
     //open the connection 
     conn.Open(); 
     //send the query to increment the number 
     cmd.ExecuteNonQuery(); 
    } 
    Label1.Text = System.Web.HttpContext.Current.User.Identity.Name; 
} 
</script> 

編輯

這工作:(或多或少)

SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["herning_brand_dk_dbConnectionString"].ConnectionString); 
SqlCommand cmd = new SqlCommand("UPDATE aspnet_Users SET LoginCounter = LoginCounter+1 WHERE UserName = @UserName", conn); 
cmd.CommandType = CommandType.Text; 
//update where UserName is x 
cmd.Parameters.AddWithValue("UserName", Login1.UserName); 
using (conn) 
{ 
    //open the connection 
    conn.Open(); 
    //send the query to increment the number 
    cmd.ExecuteNonQuery(); 
} 

它與一個名爲 「Login1」 一個全新的登錄控制。 但它不適用於我已轉換爲模板的登錄控件,即使我稱之爲「Login1」。

<asp:LoginView ID="LoginView1" runat="server"> 
    <LoggedInTemplate> 
    <b>Velkommen: &nbsp;</b> 
    <asp:LoginName ID="LoginName1" runat="server" Font-Bold="True" Font-Size="Medium" /> &nbsp; <br /> 
    <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutText="Log ud" Font-Size="Small" LogoutPageUrl="~/Default.aspx" /> &nbsp;     
    </LoggedInTemplate> 
    <AnonymousTemplate> 
    <asp:Login ID="Login1" OnLoggedIn="OnLoggedIn" runat="server"> 
     <LayoutTemplate> 
     <table border="0" cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
      <tr> 
      ..... 
      ..... 

任何建議爲什麼?

回答

0

從代碼片段我會說你使用登錄控制的表單身份驗證。正如msdn上的示例中所解釋的那樣,您可以直接從Login控制本身獲取用戶名。

然後,你的代碼應該是這樣的:

SqlCommand cmd = new SqlCommand("UPDATE aspnet_Users SET 
      LoginCounter = LoginCounter+1 WHERE UserName = @UserName", conn); 
cmd.CommandType = CommandType.Text; 
//update where UserName is Test 
cmd.Parameters.AddWithValue("UserName", Login1.UserName); 

其中Login1是你的登錄控件的ID。我不知道,如果你能在這一點上使用

System.Web.HttpContext.Current.User.Identity.Name 

,因爲身份驗證剛剛發生的回傳和上下文已初始化。我認爲您可以在認證請求完成後使用User.Identity。

+0

我試過你的榜樣,但得到的錯誤「名稱‘Login1’不在當前環境下存在」。 我檢查了登錄控件的ID = Login1。 – 2011-05-05 21:35:52

+0

不應該如此。我看到你有Label1控件,你可以從同一個塊訪問它。如果您的登錄控件具有上述ID並且它被設置爲runat =「server」,那麼您應該在您的代碼中訪問它。 – ljubomir 2011-05-06 08:32:19

+0

Label1控件實際上也不起作用,我試圖將它用於測試目的,但無法使其工作。 – 2011-05-06 08:41:12

0

如果您正在使用的登錄控制,你要的是這樣的:

MembershipUser user = Membership.GetUser(); 
string username = user.UserName;