title: How to resolve "System.UnauthorizedAccessException: Authorization for Microsoft App ID xxx failed with status code Unauthorized"
body: <p>I've created a simple bot using the .net framework. Messages are received but when replying, the application throw the exception below</p>
  
  <pre><code>System.AggregateException: One or more errors occurred. ---&gt; System.UnauthorizedAccessException: Authorization for Microsoft App ID xxx failed with status code Unauthorized and reason phrase 'Unauthorized' ---&gt; System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at Microsoft.Bot.Connector.JwtTokenRefresher.&lt;SendAsync&gt;d__2.MoveNext()
     --- End of inner exception stack trace ---
     at Microsoft.Bot.Connector.JwtTokenRefresher.&lt;SendAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Bot.Connector.BotState.&lt;GetConversationDataWithHttpMessagesAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Connector.BotStateExtensions.&lt;GetConversationDataAsync&gt;d__7.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.ConnectorStore.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IBotDataStore&lt;Microsoft-Bot-Connector-BotData&gt;-LoadAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.CachingBotDataStore.&lt;LoadFromInnerAndCache&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.CachingBotDataStore.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IBotDataStore&lt;Microsoft-Bot-Connector-BotData&gt;-LoadAsync&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.BotDataBase`1.&lt;LoadData&gt;d__16.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.BotDataBase`1.&lt;LoadAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.DialogTaskManagerBotDataLoader.&lt;LoadAsync&gt;d__11.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.ExceptionTranslationDialogTask.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.SerializeByConversation.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUser.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUser.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.LogPostToBot.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Conversation.&lt;SendAsync&gt;d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Conversation.&lt;SendAsync&gt;d__6.MoveNext()
     --- End of inner exception stack trace ---
  ---&gt; (Inner Exception #0) System.UnauthorizedAccessException: Authorization for Microsoft App ID 9d88e9f7-289e-45d2-8215-8f539955335d failed with status code Unauthorized and reason phrase 'Unauthorized' ---&gt; System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at Microsoft.Bot.Connector.JwtTokenRefresher.&lt;SendAsync&gt;d__2.MoveNext()
     --- End of inner exception stack trace ---
     at Microsoft.Bot.Connector.JwtTokenRefresher.&lt;SendAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Bot.Connector.BotState.&lt;GetConversationDataWithHttpMessagesAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Connector.BotStateExtensions.&lt;GetConversationDataAsync&gt;d__7.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.ConnectorStore.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IBotDataStore&lt;Microsoft-Bot-Connector-BotData&gt;-LoadAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.CachingBotDataStore.&lt;LoadFromInnerAndCache&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.CachingBotDataStore.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IBotDataStore&lt;Microsoft-Bot-Connector-BotData&gt;-LoadAsync&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.BotDataBase`1.&lt;LoadData&gt;d__16.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.BotDataBase`1.&lt;LoadAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.DialogTaskManagerBotDataLoader.&lt;LoadAsync&gt;d__11.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.ExceptionTranslationDialogTask.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.SerializeByConversation.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUser.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUser.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Internals.LogPostToBot.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Conversation.&lt;SendAsync&gt;d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Bot.Builder.Dialogs.Conversation.&lt;SendAsync&gt;d__6.MoveNext()&lt;---
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>open System
  open System.Text
  open Hopac
  open Hopac.Hopac
  open Microsoft.Bot.Connector
  open Suave
  open Suave.Successful
  open Suave.Filters
  open Suave.Operators
  open Microsoft.Bot.Builder.Dialogs
  open Newtonsoft.Json
  open System.Threading.Tasks
  open Microsoft.Bot.Builder.Dialogs.Internals
  
  
  type RootDialog () =
    let rec onMessageReceived (context: IDialogContext) (result: IAwaitable&lt;obj&gt;) =
      job {
  
        //let! activity = result
        do! context.PostAsync ("Sweet") |&gt; Job.awaitUnitTask
        context.Wait (ResumeAfter onMessageReceived) }
      |&gt; startAsTask |&gt; unbox
    interface IDialog&lt;obj&gt; with
      member x.StartAsync (context: IDialogContext) =
        context.Wait (ResumeAfter (onMessageReceived))
        Task.CompletedTask
  
  
  let getActivity request =
    let body = request.rawForm |&gt; Encoding.UTF8.GetString
    let activity = JsonConvert.DeserializeObject&lt;Activity&gt; (body)
    activity
  
  
  let handleSystemMessage (activity: Activity) =
    match activity.Type with 
    | ActivityTypes.DeleteUserData
    | _ -&gt; ()
  
  
  let post (activity: Activity) ctx =
    job {
      match activity.Type with
      | ActivityTypes.Message -&gt; 
          do! Conversation.SendAsync (activity, fun () -&gt; RootDialog() :&gt; _) |&gt; Job.awaitUnitTask
      | _ -&gt; handleSystemMessage activity
      return! OK "Got it" ctx }
  
  
  let part =
    choose [
      POST &gt;=&gt; path "/bot/messages" &gt;=&gt; request (getActivity &gt;&gt; (fun a b -&gt; post a b |&gt; Job.toAsync)) ]
  </code></pre>
  
  <p>PS:</p>
  
  <p>I followed the tutorial <a href="https://docs.microsoft.com/en-us/bot-framework/troubleshoot-authentication-problems" rel="nofollow noreferrer">here</a> and it fails at step 3. Everything works fine when security is disabled.</p>
  
  <p>Checking with fiddler shows that multiple requests are made to <code>login.microsoftonline.com</code> and all such requests are successful so I don't think it is the credentials. What could be the problem?</p>