ICredentialProvider - Internal Server Error
Bot Info
- SDK Platform: .NET
- SDK Version: 3.11.0.
- Deployment Environment: Localhost with Bot Emulator
Issue Description
I am trying to run the code sample shown in the following comment but keep running into ‘[500] Internal Server Error’ with “System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 401 (Unauthorized).'” thrown. https://github.com/Microsoft/BotBuilder/issues/2258#issuecomment-280506334
Is there an update to the above code sample? Basically what we are trying to achieve is avoid storing the AppID and password in the web.config file.
Code Example
https://github.com/Microsoft/BotBuilder/issues/2258#issuecomment-280506334
Reproduction Steps
- Create new Bot Application project in Visual Studio 2017
- Replace code in MessageController with the code from the GitHub comment above.
- Replace AppID and Password in the code.
- Build and run the project.
- Launch Bot-Framework-Emulator, fill in the AppID and Password and connect to the Bot on localhost
Expected Behavior
- Able to communicate with bot with no error thrown.
Actual Results
Bot emulator shows [11:11:40] -> POST 202 [conversationUpdate] [11:11:41] -> POST Request to 'http://localhost:3979/api/messages' failed: [500] Internal Server Error
Notes
Hard coding the AppID and Password in the web.config works as excepted without any errors thrown.
@Manriat could you please explain your use case for us so we can better assist you?
So this is a legitimate bug we have a fix merged already and will go out in the next release. For now you can add the following class to your project:
public static class Class1
{
public const string AppPasswordClaim = "appPassword";
public static string GetAppIdFromClaims(this ClaimsIdentity identity)
{
if (identity == null)
return null;
Claim botClaim = identity.Claims.FirstOrDefault(c => c.Type == "appid" || c.Type == "azp");
if (botClaim != null)
return botClaim.Value;
botClaim = identity.Claims.FirstOrDefault(c => c.Issuer == "https://api.botframework.com" && c.Type == "aud");
if (botClaim != null)
return botClaim.Value;
return null;
}
public static string GetAppPasswordFromClaims(this ClaimsIdentity identity)
{
return identity?.Claims.FirstOrDefault(c => c.Type == AppPasswordClaim)?.Value;
}
public static MicrosoftAppCredentials GetCredentialsFromClaims(this ClaimsIdentity claimsIdentity)
{
var appId = claimsIdentity.GetAppIdFromClaims();
var password = claimsIdentity.GetAppPasswordFromClaims();
return new MicrosoftAppCredentials(appId, password);
}
}
And update your MessagesController.cs constructor to this:
static MessagesController()
{
// Update the container to use the right MicorosftAppCredentials based on
// Identity set by BotAuthentication
var builder = new ContainerBuilder();
builder.Register(c => Class1.GetCredentialsFromClaims(((ClaimsIdentity)HttpContext.Current.User.Identity)))
.AsSelf()
.InstancePerLifetimeScope();
builder.Update(Conversation.Container);
}
Please let me know if this works for you. Good luck!
Thank you for the quick response @JasonSowers. I will test it out and let you know if I run into any issues.
@JasonSowers Just tested your fix and it works! Thanks again!
@JasonSowers I am trying to solve exactly same problem i.e. Passing AppId,Secret manually and not from web config. I followed steps mentioned this post and #2258 (comment) However, I get following exception. (Exception details from AppInsights)
POST to the bot's endpoint failed with HTTP status 500.
Exception Details :
{"method":"Microsoft.Bot.ChannelConnector.BotAPI+
Any idea what am i doing wrong? Thank you for your help!
@rohantare hard to say without seeing your code, can you put it in a repo?
Hey Jason,
Thank you for your message. Here is the link for my MultiBotPOC https://github.com/rohantare/MultiBotPOC/ Issue 01:
What I think causing issue is following code in POST method of messages controller : ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl)); var reply = activity.CreateReply( "TEST"); await client.Conversations.ReplyToActivityAsync(reply);
If I remove this piece of code, multiple bot authentication works fine.
Issue 02:
I also pass bot name to Messages controller's POST method like following (Not in my checked-in code)
public async Task
This also does not work.
Exception details from AppInsights are :
POST to the bot's endpoint failed with HTTP status 500. Exception Details : {"method":"Microsoft.Bot.ChannelConnector.BotAPI+d__29.MoveNext","level":0,"line":312,"assembly":"Microsoft.Bot.ChannelConnector, Version=3.2.0.0, Culture=neutral, PublicKeyToken=null","fileName":"d:\a\1\s\ChannelSDK\Microsoft.Bot.ChannelConnector\API\BotAPI.cs"}
Can you tell me what is causing above two issues?
Thanks a ton in advance!
Regards, Rohan
@JasonSowers I think it was my bad, I wasn't resolving the connector properly. It is working for me. Thanks!
@rohantare Can you please share the fix?
Fuck. I hate this issues tickets. A lot of searching and a lot of closed tickets without any shared help. I hate it. FUCK. Remove it from search engines, please someone.