title: ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value not returning any value
body: <p>I am creating a mvc web application to read user mails, send mail to a user and to receive the notifications using Microsoft Graph Api. My app is registered in <a href="https://apps.dev.microsoft.com" rel="nofollow noreferrer">https://apps.dev.microsoft.com</a>. I'm able to get the return value for "ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value" after login and after subscription for notifications. But I'm not able to get the return value inside the Listen() function which is used to listen for the notifications. "Object reference not set to an instance of the object" is the error given.This is the Listen() function.</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Listen()
      {
          // Validate the new subscription by sending the token back to Microsoft Graph.
          // This response is required for each subscription.
          if (Request.QueryString["validationToken"] != null)
          {               
              var token = Request.QueryString["validationToken"];
              return Content(token, "plain/text");
          }
  
          // Parse the received notifications.
          else
          {
  
              try
              {
                  var notifications = new Dictionary&lt;string, NotificationModel&gt;();
                  using (var inputStream = new System.IO.StreamReader(Request.InputStream))
                  {
                      JObject jsonObject = JObject.Parse(inputStream.ReadToEnd());
                      if (jsonObject != null)
                      {
  
                          // Notifications are sent in a 'value' array. The array might contain multiple notifications for events that are
                          // registered for the same notification endpoint, and that occur within a short timespan.
                          JArray value = JArray.Parse(jsonObject["value"].ToString());
                          foreach (var notification in value)
                          {
                              NotificationModel current = JsonConvert.DeserializeObject&lt;NotificationModel&gt;(notification.ToString());
  
                              // Check client state to verify the message is from Microsoft Graph. 
                              SubscriptionStore subscription = SubscriptionStore.GetSubscriptionInfo(current.SubscriptionId);
  
                              // This sample only works with subscriptions that are still cached.
                              if (subscription != null)
                              {
                                  if (current.ClientState == subscription.ClientState)
                                  {
  
                                      // Just keep the latest notification for each resource.
                                      // No point pulling data more than once.
                                      notifications[current.Resource] = current;
                                  }
                              }
                          }
  
                          if (notifications.Count &gt; 0)
                          {
  
                              // Query for the changed messages. 
                           await GetChangedMessagesAsync(notifications.Values);
  
                          }
                      }
                  }
              }
              catch (Exception)
              {
                  // TODO: Handle the exception.
                  // Still return a 202 so the service doesn't resend the notification.
              }
  
              return new HttpStatusCodeResult(202);
          }
      }
  
      // Get information about the changed messages and send to the browser via SignalR.
      // A production application would typically queue a background job for reliability.
      public async Task GetChangedMessagesAsync(IEnumerable&lt;NotificationModel&gt; notifications)
      {
          List&lt;MessageViewModel&gt; messages = new List&lt;MessageViewModel&gt;();
          MessageModel message = new MessageModel();
          string serviceRootUrl = "https://graph.microsoft.com/v1.0/";
          foreach (var notification in notifications)
          {
              SubscriptionStore subscription = SubscriptionStore.GetSubscriptionInfo(notification.SubscriptionId);
              string token;
              try
              {
                  // Get the access token for the subscribed user.
                  // accessToken = await GetAccessToken();
                  HttpContextBase httpContext = HttpContext;
                  token = await AuthHelper.GetAccessToken(httpContext);
              }
              catch (Exception e)
              {
                  throw e;
              }
  
              HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, serviceRootUrl + notification.Resource);
  
              // Send the 'GET' request.
              GraphHttpClient graphHttpClient = new GraphHttpClient(token);
              HttpResponseMessage response = await graphHttpClient.SendAsync(request);
  
              // Get the messages from the JSON response.
              if (response.IsSuccessStatusCode)
              {
                  string stringResult = await response.Content.ReadAsStringAsync();
                  string type = notification.ResourceData.ODataType;
                  if (type == "#Microsoft.Graph.Message")
                  {
                      message = JsonConvert.DeserializeObject&lt;MessageModel&gt;(stringResult);
                      MessageViewModel messageViewModel = new MessageViewModel(message);
                      messages.Add(messageViewModel);                       
                  }
              }
          }
          if (messages.Count &gt; 0)
          {
              NotificationService notificationService = new NotificationService();
              notificationService.SendNotificationToClient(messages);  
  
  
          }
  
  
  
      }
  </code></pre>
  
  <p>This is the GetAccessToken() method where "ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value" is used.</p>
  
  <pre><code>public static async Task&lt;string&gt; GetAccessToken(HttpContextBase HttpContext)
      {
          string accessToken = string.Empty; ;
          // Load the app config from web.config
          string appId = ConfigurationManager.AppSettings["ida:AppId"];
          string appPassword = ConfigurationManager.AppSettings["ida:AppPassword"];
          string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
          string[] scopes = ConfigurationManager.AppSettings["ida:AppScopes"]
              .Replace(' ', ',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  
          string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
  
              if (!string.IsNullOrEmpty(userId))
              {
                  // Get the user's token cache
                  SessionTokenCache tokenCache = new SessionTokenCache(userId, HttpContext);
  
                  ConfidentialClientApplication cca = new ConfidentialClientApplication(
                      appId, redirectUri, new Microsoft.Identity.Client.ClientCredential(appPassword), tokenCache.GetMsalCacheInstance(), null);
  
  
                  // Call AcquireTokenSilentAsync, which will return the cached
                  // access token if it has not expired. If it has expired, it will
                  // handle using the refresh token to get a new one.
                  AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes, cca.Users.FirstOrDefault());
  
                  accessToken = result.AccessToken;
              }
  
          // Get the current user's ID           
  
          return accessToken;
      }
  </code></pre>