У меня есть скрипт на моей странице 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();
}
Работает с новым новым элементом управления Login с именем "Login1"».Но он не работает с контролем входа, который я преобразовал в шаблон, даже когда я называю его «Логин1».
<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>
.....
.....
Есть предложения, почему?