From 57c8f24e66e2d14956d450c0baccb017a406df74 Mon Sep 17 00:00:00 2001
From: Zhiyuan Liang <141655842+zhiyuanliang-ms@users.noreply.github.com>
Date: Wed, 4 Dec 2024 10:33:49 +0800
Subject: [PATCH] use cookie auth in variant service example (#518)
---
.../HttpContextTargetingContextAccessor.cs | 53 -------------------
.../VariantServiceDemo/Pages/Index.cshtml.cs | 21 ++++++--
examples/VariantServiceDemo/Program.cs | 12 ++---
3 files changed, 21 insertions(+), 65 deletions(-)
delete mode 100644 examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs
diff --git a/examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs b/examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs
deleted file mode 100644
index 159c172f..00000000
--- a/examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright (c) Microsoft Corporation.
-// Licensed under the MIT license.
-//
-using Microsoft.FeatureManagement.FeatureFilters;
-
-namespace VariantServiceDemo
-{
- ///
- /// Provides an implementation of that creates a targeting context using info from the current HTTP request.
- ///
- public class HttpContextTargetingContextAccessor : ITargetingContextAccessor
- {
- private const string TargetingContextLookup = "HttpContextTargetingContextAccessor.TargetingContext";
- private readonly IHttpContextAccessor _httpContextAccessor;
-
- public HttpContextTargetingContextAccessor(IHttpContextAccessor httpContextAccessor)
- {
- _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
- }
-
- public ValueTask GetContextAsync()
- {
- HttpContext httpContext = _httpContextAccessor.HttpContext;
-
- //
- // Try cache lookup
- if (httpContext.Items.TryGetValue(TargetingContextLookup, out object value))
- {
- return new ValueTask((TargetingContext)value);
- }
-
- //
- // Grab username from cookie
- string username = httpContext.Request.Cookies["username"];
-
- var groups = new List();
-
- //
- // Build targeting context based on user info
- var targetingContext = new TargetingContext
- {
- UserId = username,
- Groups = groups
- };
-
- //
- // Cache for subsequent lookup
- httpContext.Items[TargetingContextLookup] = targetingContext;
-
- return new ValueTask(targetingContext);
- }
- }
-}
diff --git a/examples/VariantServiceDemo/Pages/Index.cshtml.cs b/examples/VariantServiceDemo/Pages/Index.cshtml.cs
index 6c1fb59b..8e90e656 100644
--- a/examples/VariantServiceDemo/Pages/Index.cshtml.cs
+++ b/examples/VariantServiceDemo/Pages/Index.cshtml.cs
@@ -1,6 +1,8 @@
-using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Authentication;
+using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.FeatureManagement;
+using System.Security.Claims;
namespace VariantServiceDemo.Pages
{
@@ -19,11 +21,22 @@ public IActionResult OnGet()
{
//
// generate a new visitor
- string visitor = Random.Shared.Next().ToString();
+ Username = Random.Shared.Next().ToString();
- Response.Cookies.Append("username", visitor);
+ // Clear Application Insights cookies and
+ Response.Cookies.Delete("ai_user");
+ Response.Cookies.Delete("ai_session");
- Username = visitor;
+ // Generate new user claim
+ var claims = new List
+ {
+ new Claim(ClaimTypes.Name, Username)
+ };
+
+ var identity = new ClaimsIdentity(claims, "CookieAuth");
+ var principal = new ClaimsPrincipal(identity);
+
+ HttpContext.SignInAsync("CookieAuth", principal);
return Page();
}
diff --git a/examples/VariantServiceDemo/Program.cs b/examples/VariantServiceDemo/Program.cs
index 8ac9a031..a866d02d 100644
--- a/examples/VariantServiceDemo/Program.cs
+++ b/examples/VariantServiceDemo/Program.cs
@@ -1,9 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
-using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.FeatureManagement;
-using Microsoft.FeatureManagement.Telemetry.ApplicationInsights;
using VariantServiceDemo;
var builder = WebApplication.CreateBuilder(args);
@@ -14,14 +12,12 @@
// Add services to the container.
builder.Services.AddRazorPages();
-builder.Services.AddHttpContextAccessor();
+//
+// Use cookie auth for simplicity and randomizing user
+builder.Services.AddAuthentication("CookieAuth");
builder.Services.AddApplicationInsightsTelemetry();
-//
-// App Insights TargetingId Tagging
-builder.Services.AddSingleton();
-
//
// Add variant implementations of ICalculator
builder.Services.AddSingleton();
@@ -35,7 +31,7 @@
// Including user targeting capability and the variant service provider of ICalculator which is bounded with the variant feature flag "Calculator"
// Wire up evaluation event emission
builder.Services.AddFeatureManagement()
- .WithTargeting()
+ .WithTargeting()
.WithVariantService("Calculator")
.AddApplicationInsightsTelemetry();