microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.47k stars 2.44k forks source link

ICredentialProvider - Internal Server Error #3794

Closed Manriat closed 6 years ago

Manriat commented 6 years ago

Bot Info

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

  1. Create new Bot Application project in Visual Studio 2017
  2. Replace code in MessageController with the code from the GitHub comment above.
  3. Replace AppID and Password in the code.
  4. Build and run the project.
  5. Launch Bot-Framework-Emulator, fill in the AppID and Password and connect to the Bot on localhost

Expected Behavior

  1. 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.

JasonSowers commented 6 years ago

@Manriat could you please explain your use case for us so we can better assist you?

JasonSowers commented 6 years ago

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!

Manriat commented 6 years ago

Thank you for the quick response @JasonSowers. I will test it out and let you know if I run into any issues.

Manriat commented 6 years ago

@JasonSowers Just tested your fix and it works! Thanks again!

rohantare commented 6 years ago

@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+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"}

Any idea what am i doing wrong? Thank you for your help!

JasonSowers commented 6 years ago

@rohantare hard to say without seeing your code, can you put it in a repo?

rohantare commented 6 years ago

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 Post(string botName, [FromBody]Activity activity) { if (activity.Type == ActivityTypes.Message) { ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl)); var reply = activity.CreateReply("Bot Name is: "+botName); await client.Conversations.ReplyToActivityAsync(reply); await Conversation.SendAsync(activity, () => new Dialogs.RootDialog()); } }

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

rohantare commented 6 years ago

@JasonSowers I think it was my bad, I wasn't resolving the connector properly. It is working for me. Thanks!

luisnatividade commented 6 years ago

@rohantare Can you please share the fix?

Kadae commented 6 years ago

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.