I have a form with a login and logout button and a LoginView. When I click the login button I call
Membership
.ValidateUser() and FormsAuthentication.SetAuthCookie()
after calling these Context.User.Identity.IsAuthenticated still returns false. Why? What am I missing. If I press the button a second time, Context.User.Identity.IsAuthenticated is true.
Here is the markup:
<div><%=DateTime.Now %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="btnLogout_Click" /><asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>logged in at <%=DateTime.Now %></LoggedInTemplate>
<AnonymousTemplate>logged out<%=DateTime.Now %></AnonymousTemplate>
</asp:LoginView></div>
here is the code:
protected void btnLogin_Click(object sender, EventArgs e)
{
contextUserIsAuthenticated = Context.User.Identity.IsAuthenticated;
pageUserIsAuthenticated = Page.User.Identity.IsAuthenticated;
bool validUser = Membership.ValidateUser("bar", "barbar");if (validUser)
{
FormsAuthentication.SetAuthCookie("bar", true);TextBox1.Text = "***logged in***";
}
contextUserIsAuthenticated = Context.User.Identity.IsAuthenticated;
pageUserIsAuthenticated = Page.User.Identity.IsAuthenticated;
}
protected void btnLogout_Click(object sender, EventArgs e)
{
contextUserIsAuthenticated = Context.User.Identity.IsAuthenticated;
pageUserIsAuthenticated = Page.User.Identity.IsAuthenticated;
FormsAuthentication.SignOut();TextBox1.Text = "***logged out***";
contextUserIsAuthenticated = Context.User.Identity.IsAuthenticated;
pageUserIsAuthenticated = Page.User.Identity.IsAuthenticated;
}