Skip to content

Commit

Permalink
use cookie auth in variant service example (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanliang-ms authored Dec 4, 2024
1 parent 4615186 commit 57c8f24
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 65 deletions.
53 changes: 0 additions & 53 deletions examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs

This file was deleted.

21 changes: 17 additions & 4 deletions examples/VariantServiceDemo/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<Claim>
{
new Claim(ClaimTypes.Name, Username)
};

var identity = new ClaimsIdentity(claims, "CookieAuth");
var principal = new ClaimsPrincipal(identity);

HttpContext.SignInAsync("CookieAuth", principal);

return Page();
}
Expand Down
12 changes: 4 additions & 8 deletions examples/VariantServiceDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<ITelemetryInitializer, TargetingTelemetryInitializer>();

//
// Add variant implementations of ICalculator
builder.Services.AddSingleton<ICalculator, DefaultCalculator>();
Expand All @@ -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<HttpContextTargetingContextAccessor>()
.WithTargeting()
.WithVariantService<ICalculator>("Calculator")
.AddApplicationInsightsTelemetry();

Expand Down

0 comments on commit 57c8f24

Please sign in to comment.