Skip to content

Instantly share code, notes, and snippets.

@dataneek
Last active November 23, 2015 21:34
Show Gist options
  • Save dataneek/15853deba32169cf3df0 to your computer and use it in GitHub Desktop.
Save dataneek/15853deba32169cf3df0 to your computer and use it in GitHub Desktop.
Use this to test webapi endpoints without having to build a working authentication system.
//# setup: config.MessageHandlers.Add(new DummyAuthenticationHandler());
public class DummyAuthenticationHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(Thread.CurrentPrincipal.Identity.AuthenticationType))
{
var claims =
new List<Claim>
{
new Claim(ClaimTypes.Name, "john.doe"),
new Claim(ClaimTypes.Email, "john.doe@outlook.com"),
new Claim("TenantId", "66D9BE38-3054-4C8B-9EC7-EF7782A35CA4"),
new Claim("TenantName", "New Client"),
};
var id = new ClaimsIdentity(claims, "dummy");
var principal = new ClaimsPrincipal(id);
Thread.CurrentPrincipal = principal;
if (HttpContext.Current != null)
HttpContext.Current.User = principal;
}
return base.SendAsync(request, cancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment