ClaimsIdentityEx.cs
1 
2 namespace Microsoft.Bot.Connector
3 {
4  using System.Security.Claims;
5  using System.Linq;
6 
7  public static class ClaimsIdentityEx
8  {
9  public const string AppPasswordClaim = "appPassword";
10 
16  public static string GetAppIdFromClaims(this ClaimsIdentity identity)
17  {
18  if (identity == null)
19  return null;
20 
21  // emulator adds appid claim
22  Claim botClaim = identity.Claims.FirstOrDefault(c => c.Type == "appid");
23  if (botClaim != null)
24  return botClaim.Value;
25 
26  // Fallback for BF-issued tokens
27  botClaim = identity.Claims.FirstOrDefault(c => c.Issuer == "https://api.botframework.com" && c.Type == "aud");
28  if (botClaim != null)
29  return botClaim.Value;
30 
31  return null;
32  }
33 
39  public static string GetAppPasswordFromClaims(this ClaimsIdentity identity)
40  {
41  return identity?.Claims.FirstOrDefault(c => c.Type == AppPasswordClaim)?.Value;
42  }
43 
49  public static MicrosoftAppCredentials GetCredentialsFromClaims(this ClaimsIdentity claimsIdentity)
50  {
51  var appId = claimsIdentity.GetAppIdFromClaims();
52  var password = claimsIdentity.GetAppPasswordFromClaims();
53  return new MicrosoftAppCredentials(appId, password);
54  }
55  }
56 }
static string GetAppPasswordFromClaims(this ClaimsIdentity identity)
Get the AppPassword from the Claims Identity
static string GetAppIdFromClaims(this ClaimsIdentity identity)
Get the AppId from the Claims Identity
static MicrosoftAppCredentials GetCredentialsFromClaims(this ClaimsIdentity claimsIdentity)
Get the MicrosoftAppCredentials using claims in the claims identity