I have a loginView with a buttons button to allow an anonymous user to log in and when the user is logged in a logout button will be displayed. When the user clicks the logout button the user should be logged out and the anonymous template should be displayed.
I can log the user out but the loginView continues to display the authenticated template. How do I get it to show the anonymous template?
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Label ID="Label1" runat="server" Text="Label" CssClass="user-login">Guest</asp:Label>
<button id="guestSignIn" type="button" class="btn-user bg-anonymous" data-toggle="modal" data-target="#loginModal">Login</button>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/Home-icon.png" Height="35" PostBackUrl="~/Profile.aspx" />
<asp:LoginName ID="LoginName1" runat="server" CssClass="user-login" />
<asp:Button ID="BtnLogOff" Text="Log out" CssClass="btn-user bg-user" runat="server" OnClick="LogOff" PostBackUrl="~/AboutMe.aspx" />
</LoggedInTemplate>
<RoleGroups>
<asp:RoleGroup Roles="User">
</asp:RoleGroup>
<asp:RoleGroup Roles="Admin">
<ContentTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/Home-icon.png" Height="20" CssClass=" mr-2" PostBackUrl="~/ProfilePage.aspx" />
<asp:LoginName ID="LoginName2" runat="server" CssClass="user-login" />
<asp:Button ID="BtnLogOff1" Text="Log out" CssClass="btn-user bg-admin" runat="server" OnClick="BtnLogOff_Click" PostBackUrl="~/AboutMe.aspx" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
protected void LogOff(object sender, EventArgs e)
{
Session.Clear();
Session.Abandon();
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Roles.DeleteCookie();
try
{
Session.Abandon();
FormsAuthentication.SignOut();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1000;
Response.CacheControl = "no-cache";
//Response.Redirect("login.aspx", true);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
I have tried just about everything I can think of or find.
Thanks for any help.
I figured it out. The hint was in the solution I copied and pasted from another forum. The commented line of code:
//Response.Redirect("login.aspx", true);
DaniDev said that I was only getting a partial postback. This was because I set the postback URL on the button.
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/Home-icon.png" Height="20" CssClass=" mr-2" PostBackUrl="~/ProfilePage.aspx" />
If I remove the PostBackUrl from the ImageButton tag and use the Response.Redirect method I get a complete postback and when the page reloads the LoginView reverts back to the anonymous template.
Thanx for the help DaniDev wherever you are!!