// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.Agents.AI.Hosting;
///
/// Extension methods for configuring AI hosting services in an .
///
public static class ServiceCollectionExtensions
{
///
/// Registers a that uses claims from the current user's identity
/// to generate session isolation keys.
///
/// The to add services to.
/// Optional configuration for the claims-based session isolation key provider.
/// The so that additional calls can be chained.
///
/// This method requires to be registered in the service collection.
/// Ensure that services.AddHttpContextAccessor() has been called before using this method.
///
public static IServiceCollection UseClaimsBasedSessionIsolation(
this IServiceCollection services,
ClaimsIdentitySessionIsolationKeyProviderOptions? options = null)
{
options ??= new();
ServiceDescriptor descriptor = new(typeof(SessionIsolationKeyProvider), CreateIsolationKeyProvider, ServiceLifetime.Singleton);
services.Add(descriptor);
return services;
object CreateIsolationKeyProvider(IServiceProvider serviceProvider)
{
IHttpContextAccessor contextAccessor = serviceProvider.GetRequiredService();
return new ClaimsIdentitySessionIsolationKeyProvider(contextAccessor, options);
}
}
}