title: Get UserID (not Identity.Name) inside Action Filter
body: <p>I need to find <code>UserID</code> in an <strong>Action Filter</strong> of an ASP.Net MVC application. I can get the same in a normal controller as shown below.</p>
  
  <p><a href="https://i.stack.imgur.com/nW6On.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nW6On.png" alt="enter image description here"></a></p>
  
  <p>I know how to get UserName in Action Filter.</p>
  
  <p><a href="https://i.stack.imgur.com/IA4en.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IA4en.png" alt="enter image description here"></a></p>
  
  <p>But when I try to get UserID, I get error</p>
  
  <pre><code>string userID = filterContext.HttpContext.User.Identity.GetUserID();
  </code></pre>
  
  <blockquote>
    <p>Error CS1061  'IIdentity' does not contain a definition for 'GetUserID' and no extension method 'GetUserID' accepting a first argument of type 'IIdentity' could be found</p>
  </blockquote>
  
  <p>How to get UserID inside Action Filter?</p>
  
  <p><strong>Update - REFERENCE:</strong></p>
  
  <ol>
  <li><a href="https://github.com/aspnet/Identity/blob/626362d8a22625e82a2af1b7076796f161510a97/src/Microsoft.AspNet.Identity/ClaimsIdentityExtensions.cs#L6" rel="nofollow noreferrer">ClaimsIdentityExtensions</a></li>
  </ol>
  
  <p><strong>Code for Action Filter</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;////Not used
  using System.Linq;////Not used
  using System.Web;////Not used
  using System.Web.Mvc;
  using System.Web.Routing;
  using Microsoft.AspNet.Identity; ////Not used
  
  namespace MyPortal
  {
      public class ActionFilterSessionCheck : ActionFilterAttribute, IActionFilter
      {
          void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
          {
              string userName = null;
              if (filterContext.HttpContext.User.Identity.IsAuthenticated)
              {
                  userName = filterContext.HttpContext.User.Identity.Name;
  
                  //ERROR***
                  //string userID = filterContext.HttpContext.User.Identity.GetUserID();
  
              }
  
              string sessionLogin = "Login" + userName;
              if (userName == null || (System.Web.HttpContext.Current.Session[sessionLogin] == null))
              {
  
                  if (filterContext.HttpContext.Request.IsAjaxRequest())
                  {
                      //AJAX request
                      throw new Exception("Session time out");
                  }
                  else
                  {
                      // Standard request
                      filterContext.Result = new RedirectToRouteResult(
                                         new RouteValueDictionary
                                         {
                                         { "action", "Login" },
                                         { "controller", "Account" }
                                         });
                  }
              }
  
  
              //Call standard behavior
              this.OnActionExecuting(filterContext);
          }
      }
  
  }
  </code></pre>