joe_coolish joe_coolish - 1 year ago 365
C# Question

Bot Framework Unauthorized when creating a conversation

I'm trying to resume a conversation between a bot and a user from a Web Job, and I'm getting an Unauthorized Exception.
I can successfully reply to a conversation in my MessagesController class, but when I try to run the following code in my Web Job, I get the following exception:

private static async Task SendAlertToUser(ChannelAccount botAccount, LipSenseUser user, AlertFlags alert, string extra)
Console.WriteLine($"Sending alert to user: {user.Name}");
var sb = new StringBuilder(GetTextFromAlert(alert));
if (!string.IsNullOrEmpty(extra))

var userAccount = new ChannelAccount(user.ChannelId, user.Name);
var connector = new ConnectorClient(new Uri(user.ChannelUri));

var message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;

var conversation = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

message.Conversation = new ConversationAccount(id: conversation.Id);
message.Locale = "en-Us";
message.Text = sb.ToString();

await connector.Conversations.SendToConversationAsync((Activity)message);

And the exception is:

Authorization for Microsoft App ID 58c04dd1-1234-5678-9123-456789012345 failed with status code Unauthorized and reason phrase 'Unauthorized'

When I inspect the connector's Credentials, I see that everything is correct.  I've set a breakpoint in my MessagesController and inspected the connector's Credentials from there, and everything is identical.

Also, when I look at the IntelliTrace, I see the following messages:

Bot Framework IntelliTrace Errors

My user.ChannelUri is "", which I pulled off of the user when they initialized the conversation.  Is that not the correct Uri?

Is there anything else I need to do to send a message?  My App.config appSettings looks like this:


    <add key="BotId" value="MyBotName" />
    <add key="MicrosoftAppId" value="58c04dd1-1234-5678-9123-456789012345" />
    <add key="MicrosoftAppPassword" value="5xyadfasdfaweraeFAKEasdfad" />
    <add key="AzureWebJobsStorage" value="DefaultEndpointsProtocol=https;AccountName=BLAH;AccountKey=THIS IS A KEY" />

Answer Source

Answer from Bot Framework Team on a different channel:

You need to add a call to:


This is done automatically when you are replying to a message, but for proactive messages from another process you need to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download