LoginView requires two postbacks before showing the correct template

Last post 06-17-2007 8:16 AM by e_screw. 4 replies.

Sort Posts:

  • LoginView requires two postbacks before showing the correct template

    06-16-2007, 12:08 PM
    • Loading...
    • coderGeek
    • Joined on 06-16-2007, 3:48 PM
    • Posts 63
    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;

    }

     

     

  • Re: LoginView requires two postbacks before showing the correct template

    06-16-2007, 12:40 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,370
    • Moderator
      TrustedFriends-MVPs

    for this to work for me, I've always had to do a response.redirect back to the page I'm on.  (same as you doing a postback by hand)

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: LoginView requires two postbacks before showing the correct template

    06-16-2007, 12:52 PM
    • Loading...
    • coderGeek
    • Joined on 06-16-2007, 3:48 PM
    • Posts 63

    Thanks Peter,

    I am trying not to do a full postback.  And create an AJAX efect.  I have tried to put the LoginView inside an UpdatePanel, but that also doesnt work.  I am trying to authenticate the user without redirection.  The real question is if we have called ValidateUser and ValidateUser has returned true, and we have set the cookie, what do we need to do to get Context.User.Identity.IsAuthenticated to retrun true without a full postback?

    Thanks 

     

     

  • Re: LoginView requires two postbacks before showing the correct template

    06-16-2007, 1:16 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,370
    • Moderator
      TrustedFriends-MVPs

    I don't think you can do it without a postback but I'd sure be interested to know if others know how to do it ajax style.

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: LoginView requires two postbacks before showing the correct template

    06-17-2007, 8:16 AM
    Answer
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 1:22 PM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852

    pkellner:

    I don't think you can do it without a postback but I'd sure be interested to know if others know how to do it ajax style.

    http://weblogs.asp.net/lkempe/archive/2007/01/28/login-control-in-an-asp-net-ajax-toolkit-popupcontrolextender-with-a-close-button.aspx

    Thanks

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter