title: ASP.NET Membership Login: Redirect user after logged in
body: <p>I have a web site that uses Membership and form authentication:</p>
  
  <p>ASP.NET Page:</p>
  
  <pre><code>&lt;asp:LoginView 
      ID="HeadLoginView" ...
      &lt;AnonymousTemplate&gt;
          &lt;asp:Login ID="LoginUser" OnLoggedIn="LoginUser_LoggedIn" ... &gt;
              &lt;LayoutTemplate&gt;
                  &lt;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"&gt;Username:&lt;/asp:Label&gt;
                  &lt;asp:TextBox ID="UserName" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
                  &lt;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"&gt;Password:&lt;/asp:Label&gt;
                  &lt;asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                  &lt;asp:CheckBox ID="RememberMe" runat="server"/&gt;
                  &lt;asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline"&gt;Remember me.&lt;/asp:Label&gt;
                  &lt;asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Login" style="width:100%; height:35px"/&gt;
              &lt;/LayoutTemplate&gt;
          &lt;/asp:Login&gt;            
      &lt;/AnonymousTemplate&gt;
      ...
  &lt;/asp:LoginView&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>protected void LoginUser_LoggedIn(object sender, EventArgs e)
  {
      MembershipUser user = Membership.GetUser();
      if (Roles.IsUserInRole(user.UserName, "User"))
      {
          // Do something and redirect to user page...
      }
  
      if (Roles.IsUserInRole(user.UserName, "Admin"))
      {
          // Do something and redirect to admin page...
      }
  }
  </code></pre>
  
  <p>The problem is that <code>Membership.GetUser()</code> always returns null. I tried to get username with <code>Page.User.Identity.Name</code> but it is always empty string. Is there a way to find username of currently logged in user or even better: define after-login behavior of <code>&lt;asp:LoginView&gt;</code> or <code>&lt;asp:Login&gt;</code>?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>MSDN: <code>Login.LoggedIn Event Occurs when the user logs in to the Web site and has been authenticated.</code> (<a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.loggedin.aspx" rel="nofollow">link</a>) So the problem is that why after authenticating user Membership.GetUser() returns null? </p>
  
  <p>One of my colleagues said that instead of using <code>Membership.GetUser()</code> use <code>Membership.GetUser(UserNameTextBox.Text)</code> and it is okay because user is authenticated with this username BUT the problem is that I can not find a way to get <code>Username.Text</code>. I used <code>findControl()</code> and got error.</p>
  
  <p>Any Idea?</p>