New Bot Connector support for ASP.NET Core 2.0 and 1.1

We’ve released two new nuget packages for the Microsoft Bot Connector to support .NET Core 2.0:

The above packages each target ASPNetCore 1.x and ASPNetCore 2.x respectively, and provision authentication for your bot. They must also use Microsoft.Bot.Connector.3.12.2.4 or higher.

ASP.NET core 1.1 – install AspNetCore 1.1.3.2 and use the following JSON schema in appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "MicrosoftAppId": "Your-App-Id",
  "MicrosoftAppPassword": "Your-App-Password"
} 
 

In Startup.cs, add the following to allow authentication:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(_ => Configuration);

    services.AddSingleton(typeof(ICredentialProvider), new StaticCredentialProvider(
        Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
        Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value));

    // Add framework services.
    services.AddMvc(options =>
    {
        options.Filters.Add(typeof(TrustServiceUrlAttribute));
    });
}

ASP.NET core 2.0 – install AspNetcore.2.0.0.3 and use the following JSON schema in appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "MicrosoftAppId": "Your-Microsoft-App-Id",
  "MicrosoftAppPassword": "Youru-Microsoft-App-Password"
}

In Startup.csadd the following to allow authentication:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(_ => Configuration);

    var credentialProvider = new StaticCredentialProvider(
        Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
        Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value);

    services.AddAuthentication(
            // This can be removed after https://github.com/aspnet/IISIntegration/issues/371
            options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }
        )
        .AddBotAuthentication(credentialProvider);

    services.AddSingleton(typeof(ICredentialProvider), credentialProvider);

    services.AddMvc(options =>
    {
        options.Filters.Add(typeof(TrustServiceUrlAttribute));
    });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc();
}

In MessagesController.cs, we need to make a slight change to the Post method to return a type of OkResult as follows:

[Authorize(Roles = "Bot")]
[HttpPost]
public async Task<OkResult> Post([FromBody] Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        //MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl);
        var appCredentials = new MicrosoftAppCredentials(configuration);
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl), appCredentials);

        // return our reply to the user
        var reply = activity.CreateReply("HelloWorld");
        await connector.Conversations.ReplyToActivityAsync(reply);
    }
    else
    {
        //HandleSystemMessage(activity);
    }
    return Ok();
}

To re-iterate, both of these new packages allow authentication for your bots for different versions of .NET Core, versions 1.1 and 2.0 respectively.

Happy Making!

The Bot Framework Team