title: How do I mock User.Identity.GetUserId()?
body: <p>I am trying to unit test my code which includes the line:</p>
  
  <pre><code>UserLoginInfo userIdentity = UserManager.GetLogins(User.Identity.GetUserId()).FirstOrDefault();
  </code></pre>
  
  <p>I'm just stuck on one bit as I can't get:</p>
  
  <pre><code>User.Identity.GetUserId()
  </code></pre>
  
  <p>to return a value. I have been trying the following in the set-up of my controller:</p>
  
  <pre><code>var mock = new Mock&lt;ControllerContext&gt;();
  mock.Setup(p =&gt; p.HttpContext.User.Identity.GetUserId()).Returns("string");
  </code></pre>
  
  <p>But it gives an error of "NotSupportedException was unhandled by user code". I have also tried the following:</p>
  
  <pre><code>ControllerContext controllerContext = new ControllerContext();
  
  string username = "username";
  string userid = Guid.NewGuid().ToString("N"); //could be a constant
  
  List&lt;Claim&gt; claims = new List&lt;Claim&gt;{
      new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", username), 
      new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", userid)
  };
  var genericIdentity = new GenericIdentity("Andrew");
  genericIdentity.AddClaims(claims);
  var genericPrincipal = new GenericPrincipal(genericIdentity, new string[] { });
  controllerContext.HttpContext.User = genericPrincipal;
  </code></pre>
  
  <p>Based on some code I found on stackoverflow, but this returns the same error "NotSupportedException was unhandled by user code".</p>
  
  <p>Any help as to how I proceed would be appreciated. Thanks.</p>