在 OnLoggedIn 中获取用户名并在 sql 中更新用户
我的 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: </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>
.....
.....
有什么建议吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从代码片段中我会说您使用登录控件进行表单身份验证。正如 msdn 上的示例所述 您可以直接从登录控件本身获取用户名。
然后,您的代码将如下所示:
其中 Login1 是您的登录控件的 ID。我不确定
此时是否可以使用,因为身份验证刚刚发生在回发上并且上下文已经初始化。我认为您可以在身份验证请求完成后使用 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:
where Login1 is the ID of your Login control. I'm not sure if you can use
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: