Skip to content

Add TeamsExtension sample #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f378598
Add TeamsExtension sample
rido-min Apr 22, 2025
46317a7
Merge branch 'main' into users/rido/teams-samples
rido-min Apr 22, 2025
9400b77
Return Card on Selected Item
rido-min Apr 25, 2025
b2387f2
Merge branch 'main' into users/rido/teams-samples
rido-min Apr 25, 2025
455d362
Add NuGetPackageName check and update cardValue formatting
rido-min Apr 28, 2025
5b2d4ab
Merge branch 'users/rido/teams-samples' of https://github.com/microso…
rido-min Apr 28, 2025
3964448
check commandId
rido-min Apr 28, 2025
01637ed
Merge branch 'main' into users/rido/teams-samples
rido-min Apr 29, 2025
4efec1c
merge
rido-min May 5, 2025
2efb444
add Teams Sample to SLN
rido-min May 5, 2025
bd592bc
fix ns
rido-min May 5, 2025
f8ca766
Merge branch 'main' into users/rido/teams-samples
rido-min May 6, 2025
5f0577c
apply code style to teamsagent sample
rido-min May 6, 2025
7f62f93
Merge branch 'users/rido/teams-samples' of https://github.com/microso…
rido-min May 6, 2025
b47598f
Merge branch 'main' into users/rido/teams-samples
rido-min May 6, 2025
22aab35
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 7, 2025
cb25cb8
Merge branch 'main' into users/rido/teams-samples
rido-min May 8, 2025
e2a3927
Merge branch 'users/rido/teams-samples' of https://github.com/microso…
rido-min May 8, 2025
08824e9
rm TeamsApplicationOptions
rido-min May 8, 2025
3927793
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 8, 2025
c0be831
rm allowedcallers from aspnetExt
rido-min May 8, 2025
82cd143
Merge branch 'main' into users/rido/teams-samples
rido-min May 9, 2025
651b425
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 13, 2025
4d3e7c4
merging
rido-min May 13, 2025
fcb461e
add teams sample to sln
rido-min May 13, 2025
445c6c2
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 13, 2025
afe4cd7
Merge branch 'main' into users/rido/teams-samples
rido-min May 14, 2025
1fa65cd
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 14, 2025
6e095b5
test attachments
rido-min May 16, 2025
a892f36
merging from main
rido-min May 16, 2025
637d12e
add teams agent sample to the sln
rido-min May 16, 2025
5f73efb
Merge remote-tracking branch 'origin' into users/rido/teams-samples
rido-min May 27, 2025
e86a813
Enhance build configs and improve error handling
rido-min May 27, 2025
cd01432
Add OnQueryLink handler for message extensions
rido-min May 28, 2025
d90be87
Merge branch 'main' of https://github.com/microsoft/Agents-for-net in…
rido-min May 28, 2025
ed3665b
Merge branch 'main' into users/rido/teams-samples
rido-min May 30, 2025
3969454
Merge branch 'main' into users/rido/teams-samples
rido-min Jun 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Microsoft.Agents.SDK.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32505.173
# Visual Studio Version 18
VisualStudioVersion = 18.0.10523.222 main
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Libraries", "Libraries", "{4269F3C3-6B42-419B-B64A-3E6DC0F1574A}"
EndProject
Expand Down Expand Up @@ -123,6 +123,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmptyAgent", "samples\Empty
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FullAuthentication", "samples\FullAuthentication\FullAuthentication.csproj", "{A6785A2A-A4C2-8F38-E9BB-4C5FD229F1F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamsAgent", "samples\Teams\TeamsAgent\TeamsAgent.csproj", "{D6410977-B795-C315-CC94-C2482E84BB4A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -305,6 +307,10 @@ Global
{A6785A2A-A4C2-8F38-E9BB-4C5FD229F1F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6785A2A-A4C2-8F38-E9BB-4C5FD229F1F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6785A2A-A4C2-8F38-E9BB-4C5FD229F1F9}.Release|Any CPU.Build.0 = Release|Any CPU
{D6410977-B795-C315-CC94-C2482E84BB4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6410977-B795-C315-CC94-C2482E84BB4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6410977-B795-C315-CC94-C2482E84BB4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6410977-B795-C315-CC94-C2482E84BB4A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -367,6 +373,7 @@ Global
{72503E70-31C3-82E3-1E1D-52A6B0C5A808} = {0F9D3F0D-C131-4D3B-A86F-59CC781E8A02}
{E951C602-E9ED-F77D-8FC9-5272DB90D1DD} = {674A812C-7287-4883-97F9-697D83750648}
{A6785A2A-A4C2-8F38-E9BB-4C5FD229F1F9} = {674A812C-7287-4883-97F9-697D83750648}
{D6410977-B795-C315-CC94-C2482E84BB4A} = {674A812C-7287-4883-97F9-697D83750648}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F1E8E538-309A-46F8-9CE7-AEC6589FAE60}
Expand Down
70 changes: 62 additions & 8 deletions src/samples/FullAuthentication/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Agents.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -17,6 +18,7 @@
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;

namespace FullAuthentication;
Expand Down Expand Up @@ -44,6 +46,9 @@ public static class AspNetExtensions
/// "ValidIssuers": [
/// "{default:Public-AzureBotService}"
/// ],
/// "AllowedCallers": [
/// "*"
/// ],
/// "IsGov": {optional:false},
/// "AzureBotServiceOpenIdMetadataUrl": optional,
/// "OpenIdMetadataUrl": optional,
Expand All @@ -58,12 +63,14 @@ public static class AspNetExtensions
/// `AzureBotServiceOpenIdMetadataUrl` can be omitted. In which case default values in combination with `IsGov` is used.
/// `OpenIdMetadataUrl` can be omitted. In which case default values in combination with `IsGov` is used.
/// `AzureBotServiceTokenHandling` defaults to true and should always be true until Azure Bot Service sends Entra ID token.
/// `AllowedCallers` is optional and defaults to "*". Otherwise, a list of AppId's the Agent will accept requests from.
/// </remarks>
public static void AddAgentAspNetAuthentication(this IServiceCollection services, IConfiguration configuration, string tokenValidationSectionName = "TokenValidation", ILogger logger = null)
public static void AddAgentAspNetAuthentication(this IServiceCollection services, IConfiguration configuration, string tokenValidationSectionName = "TokenValidation", ILogger logger = null!)
{
IConfigurationSection tokenValidationSection = configuration.GetSection(tokenValidationSectionName);
List<string> validTokenIssuers = tokenValidationSection.GetSection("ValidIssuers").Get<List<string>>();
List<string> audiences = tokenValidationSection.GetSection("Audiences").Get<List<string>>();
List<string> validTokenIssuers = tokenValidationSection.GetSection("ValidIssuers")!.Get<List<string>>()!;
List<string> allowedCallers = tokenValidationSection.GetSection("AllowedCallers")!.Get<List<string>>()!;
List<string> audiences = tokenValidationSection.GetSection("Audiences")!.Get<List<string>>()!;

if (!tokenValidationSection.Exists())
{
Expand All @@ -85,7 +92,7 @@ public static void AddAgentAspNetAuthentication(this IServiceCollection services
"https://login.microsoftonline.com/69e9b82d-4842-4902-8d1e-abc5b98a55e8/v2.0",
];

string tenantId = tokenValidationSection["TenantId"];
string tenantId = tokenValidationSection["TenantId"]!;
if (!string.IsNullOrEmpty(tenantId))
{
validTokenIssuers.Add(string.Format(CultureInfo.InvariantCulture, AuthenticationConstants.ValidTokenIssuerUrlTemplateV1, tenantId));
Expand All @@ -102,21 +109,26 @@ public static void AddAgentAspNetAuthentication(this IServiceCollection services
bool azureBotServiceTokenHandling = tokenValidationSection.GetValue("AzureBotServiceTokenHandling", true);

// If the `AzureBotServiceOpenIdMetadataUrl` setting is not specified, use the default based on `IsGov`. This is what is used to authenticate ABS tokens.
string azureBotServiceOpenIdMetadataUrl = tokenValidationSection["AzureBotServiceOpenIdMetadataUrl"];
string azureBotServiceOpenIdMetadataUrl = tokenValidationSection["AzureBotServiceOpenIdMetadataUrl"]!;
if (string.IsNullOrEmpty(azureBotServiceOpenIdMetadataUrl))
{
azureBotServiceOpenIdMetadataUrl = isGov ? AuthenticationConstants.GovAzureBotServiceOpenIdMetadataUrl : AuthenticationConstants.PublicAzureBotServiceOpenIdMetadataUrl;
}

// If the `OpenIdMetadataUrl` setting is not specified, use the default based on `IsGov`. This is what is used to authenticate Entra ID tokens.
string openIdMetadataUrl = tokenValidationSection["OpenIdMetadataUrl"];
string openIdMetadataUrl = tokenValidationSection["OpenIdMetadataUrl"]!;
if (string.IsNullOrEmpty(openIdMetadataUrl))
{
openIdMetadataUrl = isGov ? AuthenticationConstants.GovOpenIdMetadataUrl : AuthenticationConstants.PublicOpenIdMetadataUrl;
}

TimeSpan openIdRefreshInterval = tokenValidationSection.GetValue("OpenIdMetadataRefresh", BaseConfigurationManager.DefaultAutomaticRefreshInterval);

_ = services.AddAuthorization(options =>
{
options.AddPolicy("AllowedCallers", policy => policy.Requirements.Add(new AllowedCallersPolicy(allowedCallers)));
});

_ = services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
Expand Down Expand Up @@ -155,7 +167,7 @@ public static void AddAgentAspNetAuthentication(this IServiceCollection services
return;
}

string[] parts = authorizationHeader?.Split(' ');
string[] parts = authorizationHeader?.Split(' ')!;
if (parts.Length != 2 || parts[0] != "Bearer")
{
// Default to AadTokenValidation handling
Expand All @@ -165,7 +177,7 @@ public static void AddAgentAspNetAuthentication(this IServiceCollection services
}

JwtSecurityToken token = new(parts[1]);
string issuer = token.Claims.FirstOrDefault(claim => claim.Type == AuthenticationConstants.IssuerClaim)?.Value;
string issuer = token.Claims.FirstOrDefault(claim => claim.Type == AuthenticationConstants.IssuerClaim)?.Value!;

if (azureBotServiceTokenHandling && AuthenticationConstants.BotFrameworkTokenIssuer.Equals(issuer))
{
Expand Down Expand Up @@ -210,4 +222,46 @@ public static void AddAgentAspNetAuthentication(this IServiceCollection services
};
});
}

class AllowedCallersPolicy(IList<string> allowedCallers) : IAuthorizationHandler, IAuthorizationRequirement
{
private readonly IList<string> _allowedCallers = allowedCallers ?? [];

public Task HandleAsync(AuthorizationHandlerContext context)
{
if (_allowedCallers.Count == 0 || _allowedCallers[0] == "*")
{
context.Succeed(this);
return Task.CompletedTask;
}

List<Claim> claims = [.. context.User.Claims];

// allow ABS
var issuer = claims.SingleOrDefault(claim => claim.Type == AuthenticationConstants.IssuerClaim);
if (AuthenticationConstants.BotFrameworkTokenIssuer.Equals(issuer))
{
context.Succeed(this);
}
else
{
// Get azp or appid claim
var party = claims.SingleOrDefault(claim => claim.Type == AuthenticationConstants.AuthorizedParty);
party ??= claims.SingleOrDefault(claim => claim.Type == AuthenticationConstants.AppIdClaim);

// party must be in allowed list
bool isAllowed = party != null && _allowedCallers.Where(allowed => allowed == party.Value).Any();
if (isAllowed)
{
context.Succeed(this);
}
else
{
context.Fail();
}
}

return Task.CompletedTask;
}
}
}
Loading
Loading