Microsoft.Bot.Connector.AspNetCore/BotAuthentication.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Net.Http;
6 using System.Security.Claims;
7 using System.Text.Encodings.Web;
8 using System.Threading;
9 using System.Threading.Tasks;
10 
11 using Microsoft.AspNetCore.Authentication;
12 using Microsoft.AspNetCore.Authentication.JwtBearer;
13 using Microsoft.AspNetCore.Builder;
14 using Microsoft.AspNetCore.Http;
15 using Microsoft.AspNetCore.Http.Authentication;
16 using Microsoft.AspNetCore.Http.Features.Authentication;
17 using Microsoft.AspNetCore.Mvc.Filters;
18 using Microsoft.Extensions.Logging;
19 using Microsoft.Extensions.Options;
20 using Newtonsoft.Json.Linq;
21 
22 namespace Microsoft.Bot.Connector
23 {
28  {
29  public static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, string microsoftAppId, string microsoftAppPassword)
30  {
31  if (app == null)
32  {
33  throw new ArgumentNullException(nameof(app));
34  }
35 
36  return app.UseBotAuthentication(new StaticCredentialProvider(microsoftAppId, microsoftAppPassword));
37  }
38 
39  public static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, ICredentialProvider credentialProvider)
40  {
41  if (app == null)
42  {
43  throw new ArgumentNullException(nameof(app));
44  }
45 
46  var options = new BotAuthenticationOptions
47  {
48  CredentialProvider = credentialProvider
49  };
50 
51  return app.UseMiddleware<BotAuthenticationMiddleware>(Options.Create(options));
52  }
53 
54  public static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, BotAuthenticationOptions options)
55  {
56  if (app == null)
57  {
58  throw new ArgumentNullException(nameof(app));
59  }
60  if (options == null)
61  {
62  throw new ArgumentNullException(nameof(options));
63  }
64 
65  return app.UseMiddleware<BotAuthenticationMiddleware>(Options.Create(options));
66  }
67  }
68 
69 
73  public sealed class BotAuthenticationMiddleware : AuthenticationMiddleware<BotAuthenticationOptions>
74  {
75  public BotAuthenticationMiddleware(RequestDelegate next, IOptions<BotAuthenticationOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder)
76  : base(next, options, loggerFactory, encoder)
77  {
78  if (next == null)
79  {
80  throw new ArgumentNullException(nameof(next));
81  }
82 
83  if (loggerFactory == null)
84  {
85  throw new ArgumentNullException(nameof(loggerFactory));
86  }
87 
88  if (encoder == null)
89  {
90  throw new ArgumentNullException(nameof(encoder));
91  }
92 
93  if (options == null)
94  {
95  throw new ArgumentNullException(nameof(options));
96  }
97  }
98 
99  protected override AuthenticationHandler<BotAuthenticationOptions> CreateHandler()
100  {
101  return new BotAuthenticationHandler();
102  }
103  }
104 }
override AuthenticationHandler< BotAuthenticationOptions > CreateHandler()
static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, ICredentialProvider credentialProvider)
Options for BotAuthenticationMiddleware.
Extension methods to add BotAuthentication capabilities to an HTTP application pipeline.
Bot authentication hanlder used by BotAuthenticationMiddleware.
Bot authentication middleware. AuthenticationMiddleware<TOptions> for more information.
static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, string microsoftAppId, string microsoftAppPassword)
BotAuthenticationMiddleware(RequestDelegate next, IOptions< BotAuthenticationOptions > options, ILoggerFactory loggerFactory, UrlEncoder encoder)
static IApplicationBuilder UseBotAuthentication(this IApplicationBuilder app, BotAuthenticationOptions options)
Static credential provider which has the appid and password static