title: Logging into web app doesn't always show that I'm logged in
body: <p>I been running into some weird issues with a site a I deployed. The site is using Visual Studio 2013 Webform template, so it is using ASPMembership Database as well. So during a bunch of testing I have noticed that after logging in I see this in my title bar. Sometimes it shows that I am logged in but most of the time it doesn't show that I am logged in, it even happens when I run it locally.</p>
  
  <p>I have a number of references in the project for OAuth and SimpleOAuth, thats for importing of contacts from Yahoo and Hotmail and I am not sure if that would be the cause of it.</p>
  
  <p>I attached a pic of how it looks after I log in. Where you see the word Login, it should be Logout and Register should be my email address, but low and behold it doesn't. So this is screwing up alot of my code. I have tested it in IE11 and Chrome.</p>
  
  <p>Has anyone ever seen this happen before or have heard of what is going wrong?</p>
  
  <p><a href="https://i.stack.imgur.com/X5clG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X5clG.png" alt="After Login"></a></p>
  
  <p><strong>Login Code</strong></p>
  
  <pre><code>    using System;
  using System.Web;
  using System.Web.UI;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.Owin;
  using Owin;
  using GSDataCollection.Models;
  
  namespace GSDataCollection.Account
  {
      public partial class Login : Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              RegisterHyperLink.NavigateUrl = "Register";
              // Enable this once you have account confirmation enabled for password reset functionality
              //ForgotPasswordHyperLink.NavigateUrl = "Forgot";
              OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
              var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
              if (!String.IsNullOrEmpty(returnUrl))
              {
                  RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
              }
          }
  
          protected void LogIn(object sender, EventArgs e)
          {
              if (IsValid)
              {
                  // Validate the user password
                  var manager = Context.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
                  var signinManager = Context.GetOwinContext().GetUserManager&lt;ApplicationSignInManager&gt;();
  
                  // This doen't count login failures towards account lockout
                  // To enable password failures to trigger lockout, change to shouldLockout: true
                  var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
  
                  switch (result)
                  {
                      case SignInStatus.Success:
                          Response.Redirect("~/Home.aspx");
                          //IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                          break;
                      case SignInStatus.LockedOut:
                          Response.Redirect("/Account/Lockout");
                          break;
                      case SignInStatus.RequiresVerification:
                          Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&amp;RememberMe={1}", 
                                                          Request.QueryString["ReturnUrl"],
                                                          RememberMe.Checked),
                                            true);
                          break;
                      case SignInStatus.Failure:
                      default:
                          FailureText.Text = "Invalid login attempt";
                          ErrorMessage.Visible = true;
                          break;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>as you can see I made one switch in this code and that is redirecting to a different page and commenting out the IdentityHelper and its this part..</p>
  
  <pre><code>    case SignInStatus.Success:
      Response.Redirect("~/Home.aspx");
      //IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
      break;
  </code></pre>
  
  <p>If I uncomment the IdentityHelper.RedirectToReturnUrl I get this code..</p>
  
  <pre><code>    using System;
  using System.Security.Claims;
  using System.Threading.Tasks;
  using System.Web;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using Microsoft.AspNet.Identity.Owin;
  using Microsoft.Owin.Security;
  using GSDataCollection.Models;
  
  namespace GSDataCollection.Models
  {
      // You can add User data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
      public class ApplicationUser : IdentityUser
      {
          public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
          {
              // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
              // Add custom user claims here
              return userIdentity;
          }
  
          public Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(ApplicationUserManager manager)
          {
              return Task.FromResult(GenerateUserIdentity(manager));
          }
      }
  
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public ApplicationDbContext()
              : base("DefaultConnection", throwIfV1Schema: false)
          {
          }
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  }
  
  #region Helpers
  namespace GSDataCollection
  {
      public static class IdentityHelper
      {
          // Used for XSRF when linking external logins
          public const string XsrfKey = "XsrfId";
  
          public static void SignIn(ApplicationUserManager manager, ApplicationUser user, bool isPersistent)
          {
              IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
              authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
              var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
              authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
          }
  
          public const string ProviderNameKey = "providerName";
          public static string GetProviderNameFromRequest(HttpRequest request)
          {
              return request.QueryString[ProviderNameKey];
          }
  
          public const string CodeKey = "code";
          public static string GetCodeFromRequest(HttpRequest request)
          {
              return request.QueryString[CodeKey];
          }
  
          public const string UserIdKey = "userId";
          public static string GetUserIdFromRequest(HttpRequest request)
          {
              return HttpUtility.UrlDecode(request.QueryString[UserIdKey]);
          }
  
          public static string GetResetPasswordRedirectUrl(string code, HttpRequest request)
          {
              var absoluteUri = "/Account/ResetPassword?" + CodeKey + "=" + HttpUtility.UrlEncode(code);
              return new Uri(request.Url, absoluteUri).AbsoluteUri.ToString();
          }
  
          public static string GetUserConfirmationRedirectUrl(string code, string userId, HttpRequest request)
          {
              var absoluteUri = "/Account/Confirm?" + CodeKey + "=" + HttpUtility.UrlEncode(code) + "&amp;" + UserIdKey + "=" + HttpUtility.UrlEncode(userId);
              return new Uri(request.Url, absoluteUri).AbsoluteUri.ToString();
          }
  
          private static bool IsLocalUrl(string url)
          {
              return !string.IsNullOrEmpty(url) &amp;&amp; ((url[0] == '/' &amp;&amp; (url.Length == 1 || (url[1] != '/' &amp;&amp; url[1] != '\\'))) || (url.Length &gt; 1 &amp;&amp; url[0] == '~' &amp;&amp; url[1] == '/'));
          }
  
          public static void RedirectToReturnUrl(string returnUrl, HttpResponse response)
          {
              if (!String.IsNullOrEmpty(returnUrl) &amp;&amp; IsLocalUrl(returnUrl))
              {
                  response.Redirect(returnUrl);
              }
              else
              {
                  response.Redirect("~/");
              }
          }
      }
  }
  #endregion
  </code></pre>
  
  <p>I have no idea where to put my redirect to Home.aspx in here that is why I commented out the IdentityHelper.RedirectToReturnUrl</p>
  
  <p><strong>Code from MasterPage that renders the file menu</strong></p>
  
  <pre><code>    &lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="GSDataCollection.SiteMaster" %&gt;
  &lt;%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %&gt;
  &lt;!DOCTYPE html&gt;
  
  &lt;html lang="en"&gt;
  &lt;head runat="server"&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
      &lt;title&gt;&lt;%: Page.Title %&gt; - My ASP.NET Application&lt;/title&gt;
  
      &lt;asp:PlaceHolder runat="server"&gt;
          &lt;%: Scripts.Render("~/bundles/modernizr") %&gt;
      &lt;/asp:PlaceHolder&gt;
      &lt;webopt:bundlereference runat="server" path="~/Content/css" /&gt;
      &lt;link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form runat="server"&gt;
          &lt;%--&lt;telerik:RadScriptManager runat="server" ID="RadScriptManager1"&gt;&lt;/telerik:RadScriptManager&gt;--%&gt;
          &lt;asp:ScriptManager runat="server"&gt;
              &lt;Scripts&gt;
                  &lt;%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%&gt;
                  &lt;%--Framework Scripts--%&gt;
                  &lt;asp:ScriptReference Name="MsAjaxBundle" /&gt;
                  &lt;asp:ScriptReference Name="jquery" /&gt;
                  &lt;asp:ScriptReference Name="bootstrap" /&gt;
                  &lt;asp:ScriptReference Name="respond" /&gt;
                  &lt;asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" /&gt;
                  &lt;asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" /&gt;
                  &lt;asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" /&gt;
                  &lt;asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" /&gt;
                  &lt;asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" /&gt;
                  &lt;asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" /&gt;
                  &lt;asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" /&gt;
                  &lt;asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" /&gt;
                  &lt;asp:ScriptReference Name="WebFormsBundle" /&gt;
                  &lt;%--Site Scripts--%&gt;
              &lt;/Scripts&gt;
          &lt;/asp:ScriptManager&gt;
  
          &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
              &lt;div class="container"&gt;
                  &lt;div class="navbar-header"&gt;
                      &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
                          &lt;span class="icon-bar"&gt;&lt;/span&gt;
                          &lt;span class="icon-bar"&gt;&lt;/span&gt;
                          &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;/button&gt;
                      &lt;a class="navbar-brand" runat="server" href="~/"&gt;&lt;font color="white"&gt;Umbizo&lt;/font&gt;&lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="navbar-collapse collapse"&gt;
                      &lt;ul class="nav navbar-nav"&gt;
                          &lt;li&gt;&lt;a runat="server" href="~/"&gt;&lt;font color="white"&gt;Home&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a runat="server" href="~/About"&gt;&lt;font color="white"&gt;About&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a runat="server" href="~/Contact"&gt;&lt;font color="white"&gt;Contact&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                      &lt;/ul&gt;
                      &lt;asp:LoginView runat="server" ViewStateMode="Disabled"&gt;
                          &lt;AnonymousTemplate&gt;
                              &lt;ul class="nav navbar-nav navbar-right"&gt;
                                  &lt;li&gt;&lt;a runat="server" href="~/Account/Register"&gt;&lt;font color="white"&gt;Register&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;&lt;a runat="server" href="~/Account/Login"&gt;&lt;font color="white"&gt;Log in&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/AnonymousTemplate&gt;
                          &lt;LoggedInTemplate&gt;
                              &lt;ul class="nav navbar-nav navbar-right"&gt;
                                  &lt;li&gt;&lt;a runat="server" href="~/Account/Manage" title="Manage your account"&gt;&lt;font color="white"&gt;Hello, &lt;%: Context.User.Identity.GetUserName()  %&gt; !&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;
                                      &lt;asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" ForeColor="White" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" /&gt;
                                  &lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/LoggedInTemplate&gt;
                      &lt;/asp:LoginView&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="container body-content"&gt;
              &lt;asp:ContentPlaceHolder ID="MainContent" runat="server"&gt;
              &lt;/asp:ContentPlaceHolder&gt;
              &lt;hr /&gt;
              &lt;footer&gt;
                  &lt;p&gt;&amp;copy; &lt;%: DateTime.Now.Year %&gt; - Umbizo&lt;/p&gt;
              &lt;/footer&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Code that is needed for some parts of the above code in the MasterPage</strong></p>
  
  <pre><code>    #region Assembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0
  // C:\Users\Simon\Documents\Visual Studio 2013\Projects\GSDataCollection\packages\Microsoft.AspNet.Identity.Core.2.1.0\lib\net45\Microsoft.AspNet.Identity.Core.dll
  #endregion
  
  using System;
  using System.Runtime.CompilerServices;
  using System.Security.Claims;
  using System.Security.Principal;
  
  namespace Microsoft.AspNet.Identity
  {
      // Summary:
      //     Extensions making it easier to get the user name/user id claims off of an
      //     identity
      public static class IdentityExtensions
      {
          // Summary:
          //     Return the claim value for the first claim with the specified type if it
          //     exists, null otherwise
          //
          // Parameters:
          //   identity:
          //
          //   claimType:
          public static string FindFirstValue(this ClaimsIdentity identity, string claimType);
          //
          // Summary:
          //     Return the user id using the UserIdClaimType
          //
          // Parameters:
          //   identity:
          //
          // Type parameters:
          //   T:
          public static T GetUserId&lt;T&gt;(this IIdentity identity) where T : IConvertible;
          //
          // Summary:
          //     Return the user id using the UserIdClaimType
          //
          // Parameters:
          //   identity:
          public static string GetUserId(this IIdentity identity);
          //
          // Summary:
          //     Return the user name using the UserNameClaimType
          //
          // Parameters:
          //   identity:
          public static string GetUserName(this IIdentity identity);
      }
  }
  </code></pre>