开发者

Get username in OnLoggedIn and update user in sql

开发者 https://www.devze.com 2023-03-03 12:02 出处:网络
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.

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: &nbsp;</b>
    <asp:LoginName ID="LoginName1" runat="server" Font-Bold="True" Font-Size="Medium" /> &nbsp;  <br /> 
    <asp:LoginStatus ID="LoginStatus1" r开发者_JAVA技巧unat="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>
          .....
          .....

Any suggestions why?


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消