title: botframework 401 Unauthorized
body: <p>I've been trying MS bot framework for a couple of weeks and it worked great, but today I got this 401 Unauthorized error.</p>
  
  <p>If I set up empty values for MicrosoftAppId and MicrosoftAppPassword then it works getting a 202 Accepted Code and JSON.</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "type": "message",
    "timestamp": "2017-05-05T19:16:05.9383241Z",
    "serviceUrl": "http://localhost:9000/",
    "channelId": "emulator",
    "from": {
      "id": "56800324",
      "name": "Bot1"
    },
    "conversation": {
      "isGroup": false,
      "id": "8a684db8",
      "name": "Conv1"
    },
    "recipient": {
      "id": "2c1c7fa3",
      "name": "User1"
    },
    "text": "Has dicho: 55 tiene 2 caracteres. De momento no se hacer más.",
    "attachments": [],
    "entities": [],
    "replyToId": "4e4621d62e544a99aa1ea385b12d536f"
  }
  </code></pre>
  
  <p>If I setup a value for MicrosoftAppId and MicrosoftAppPassword (yes, I have checked it several times at dev.botframework.com and both of then are right) then it doesn't work:</p>
  
  <pre class="lang-none prettyprint-override"><code>401 Unathorized
  Connection: Keep-Alive
  Content-Length: 540
  Content-type: application/json ; charset=utf-8
  Host: Localhost:9000
  User-Agent: Microsoft.Bot.Connector.ConnectorClient/3.5.3.0 Microsoft-BotFramework/3.1 (BotBuilder .Net/3.5.3.0)
  </code></pre>
  
  <p>and JSON:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "type": "message",
    "timestamp": "2017-05-05T19:19:15.4091892Z",
    "serviceUrl": "http://localhost:9000/",
    "channelId": "emulator",
    "from": {
      "id": "56800324",
      "name": "Bot1"
    },
    "conversation": {
      "isGroup": false,
      "id": "8a684db8",
      "name": "Conv1"
    },
    "recipient": {
      "id": "2c1c7fa3",
      "name": "User1"
    },
    "text": "Has dicho: sdfs tiene 4 caracteres. De momento no se hacer más.",
    "attachments": [],
    "entities": [],
    "replyToId": "0adec452277a489e9acc5b4403fa5965"
  }
  </code></pre>
  
  <p>If I test connection to my bot at dev.botframework.com I also get Unauthorized.</p>
  
  <p>Any ideas?<br>
  Thanks!</p>
  
  <p>UPDATE:
  This is my MessagesController Class:</p>
  
  <pre><code> namespace Geni.Controllers
  {
      [BotAuthentication]
      public class MessagesController : ApiController
      {
          /// &lt;summary&gt;
          /// POST: api/Messages
          /// Receive a message from a user and reply to it
          /// &lt;/summary&gt;
          public async Task&lt;HttpResponseMessage&gt; Post([FromBody]Activity activity)
          {  
              if (activity.Type == ActivityTypes.Message)
              {
                  ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                  // calculate something for us to return
                  int length = (activity.Text ?? string.Empty).Length;
  
                  // return our reply to the user
                  Activity reply = activity.CreateReply($"Has dicho: {activity.Text} tiene {length} caracteres. De momento no se hacer más.");
                  await connector.Conversations.ReplyToActivityAsync(reply);
              }
              else
              {
                  HandleSystemMessage(activity);
              }
              var response = Request.CreateResponse(HttpStatusCode.OK);
              return response;
  
          }
          private Activity HandleSystemMessage(Activity message)
          {
              if (message.Type == ActivityTypes.DeleteUserData)
              {
                  // Implement user deletion here
                  // If we handle user deletion, return a real message
              }
              else if (message.Type == ActivityTypes.ConversationUpdate)
              {
                  // Handle conversation state changes, like members being added and removed
                  // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                  // Not available in all channels
              }
              else if (message.Type == ActivityTypes.ContactRelationUpdate)
              {
                  // Handle add/remove from contact lists
                  // Activity.From + Activity.Action represent what happened
              }
              else if (message.Type == ActivityTypes.Typing)
              {
                  // Handle knowing tha the user is typing
              }
              else if (message.Type == ActivityTypes.Ping)
              {
              }
  
              return null;
          }
      }
  }
  </code></pre>