Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll
Last active March 27, 2025 18:30
Show Gist options
  • Save chrisfcarroll/a15ef85471df2f93ecb179b4025aa943 to your computer and use it in GitHub Desktop.
Save chrisfcarroll/a15ef85471df2f93ecb179b4025aa943 to your computer and use it in GitHub Desktop.
A Fake Claims Identity for testing, with claims settable in code.
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Principal;
public class FakeClaimsIdentity : ClaimsIdentity
{
public readonly List<Claim> ClaimsValue = new List<Claim>();
public bool IsAuthenticatedSet;
public FakeClaimsIdentity() : base(new GenericIdentity(typeof(FakeClaimsIdentity).Name)) { }
public FakeClaimsIdentity(string name) : base(new GenericIdentity(name)) { }
public FakeClaimsIdentity(GenericIdentity identity) : base(identity) { }
public override bool IsAuthenticated => IsAuthenticatedSet;
public override IEnumerable<Claim> Claims => ClaimsValue;
public FakeClaimsIdentity WithClaim(Claim claim)
{
AddClaim(claim);
return this;
}
public override void AddClaim(Claim claim) { ClaimsValue.Add(claim); }
public override void AddClaims(IEnumerable<Claim> claims) { ClaimsValue.AddRange(claims); }
public override bool TryRemoveClaim(Claim claim) { return ClaimsValue.Remove(claim); }
}
@chrisfcarroll
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment