在 OnLoggedIn 中获取用户名并在 sql 中更新用户

发布于 2024-11-05 01:53:18 字数 2504 浏览 0 评论 0原文

我的 site.master 页面中有一个更新 SQL 的脚本,它工作正常,如下所示,但我不想更新 Test 我想更新刚刚执行的用户已登录。

如何选择当前用户?

我发现了以下内容,但不知道它是否正确,以及应该添加到哪里:

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>
          .....
          .....

有什么建议吗?

I have a script in my site.master page that updates an SQL, it works fine as shown below, but instead of updating the Test I want to update the user who just logged in.

How do I select the current user?

I've found the following, but do not know if it's right, and where it should be added:

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

I use Forms Authentication.

<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>

EDIT

This works: (more or less)

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();
}

It works with a fresh new Login control named "Login1".
But it does not work with the login control I have converted to template, even when I call it "Login1".

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

Any suggestions why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-11-12 01:53:18

从代码片段中我会说您使用登录控件进行表单身份验证。正如 msdn 上的示例所述 您可以直接从登录控件本身获取用户名。

然后,您的代码将如下所示:

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 。

From the code snippet i would say that you use Login control for the forms authentication. As explained in this example on msdn you can get the username directly from the Login control itself.

Then, your code would look like this:

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);

where Login1 is the ID of your Login control. I'm not sure if you can use

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

at this point because the authentication just occured on the PostBack and the Context is already initialized. I think you can use the User.Identity after the authentication Request is finished.

也只是曾经 2024-11-12 01:53:18

如果您使用登录控件,您想要的是这样的:

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

If you are using the Login control, what you want is this:

MembershipUser user = Membership.GetUser();
string username = user.UserName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文