StateClientEx.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Net.Http;
4 
5 #if NET45
6 using System.Security.Claims;
7 using System.Threading;
8 using System.Web;
9 #endif
10 
11 namespace Microsoft.Bot.Connector
12 {
13  public partial class StateClient
14  {
22  public StateClient(Uri baseUri, string microsoftAppId = null, string microsoftAppPassword = null, params DelegatingHandler[] handlers)
23  : this(baseUri, new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword), handlers: handlers)
24  {
25  }
26 
34  public StateClient(Uri baseUri, MicrosoftAppCredentials credentials, bool addJwtTokenRefresher = true, params DelegatingHandler[] handlers)
35  : this(baseUri, addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials) : handlers)
36  {
37  this.Credentials = credentials;
38  }
39 
40 #if NET45
41  public StateClient(Uri baseUri, ICredentialProvider credentialProvider, ClaimsIdentity claimsIdentity = null, params DelegatingHandler[] handlers)
49  : this(baseUri, handlers: handlers)
50  {
51  if (claimsIdentity == null)
52  claimsIdentity = HttpContext.Current.User.Identity as ClaimsIdentity ?? Thread.CurrentPrincipal.Identity as ClaimsIdentity;
53 
54  if (claimsIdentity == null)
55  throw new ArgumentNullException("ClaimsIdentity not passed in and not available via Thread.CurrentPrincipal.Identity");
56 
57  var appId = claimsIdentity.GetAppIdFromClaims();
58  var password = credentialProvider.GetAppPasswordAsync(appId).Result;
59  this.Credentials = new MicrosoftAppCredentials(appId, password);
60  }
61 
68  public StateClient(ICredentialProvider credentialProvider, ClaimsIdentity claimsIdentity = null, params DelegatingHandler[] handlers)
69  : this(null, credentialProvider, claimsIdentity, handlers: handlers)
70  {
71  }
72 #endif
73 
81  public StateClient(MicrosoftAppCredentials credentials, bool addJwtTokenRefresher = true, params DelegatingHandler[] handlers)
82  : this(addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials) : handlers)
83  {
84  this.Credentials = credentials;
85  }
86 
87  private static DelegatingHandler[] AddJwtTokenRefresher(DelegatingHandler[] srcHandlers, MicrosoftAppCredentials credentials)
88  {
89  var handlers = new List<DelegatingHandler>(srcHandlers);
90  handlers.Add(new JwtTokenRefresher(credentials));
91  return handlers.ToArray();
92  }
93 
94  partial void CustomInitialize()
95  {
96  ConnectorClient.AddUserAgent(this);
97  }
98  }
99 }
StateClient(Uri baseUri, MicrosoftAppCredentials credentials, bool addJwtTokenRefresher=true, params DelegatingHandler[] handlers)
Create a new instance of the StateClient class
StateClient(MicrosoftAppCredentials credentials, bool addJwtTokenRefresher=true, params DelegatingHandler[] handlers)
Create a new instance of the StateClient class
Task< string > GetAppPasswordAsync(string appId)
Get the app password for a given bot appId, if it is not a valid appId, return Null ...
The Bot State REST API allows your bot to store and retrieve state associated with conversations cond...
Definition: StateClient.cs:57
´╗┐The Bot Connector REST API allows your bot to send and receive messages to channels configured in t...
StateClient(Uri baseUri, string microsoftAppId=null, string microsoftAppPassword=null, params DelegatingHandler[] handlers)
Create a new instance of the StateClient class