Skip to content

Instantly share code, notes, and snippets.

@ThatRendle
Created September 22, 2015 16:26
Show Gist options
  • Save ThatRendle/546141740f244dcfba88 to your computer and use it in GitHub Desktop.
Save ThatRendle/546141740f244dcfba88 to your computer and use it in GitHub Desktop.
public class ClaimsPrincipalBuilder
{
public static ClaimsPrincipal FromDictionary(IDictionary<string, string> source)
{
var claims = source.Select(pair => new Claim(pair.Key, pair.Value));
var identity = new ClaimsIdentity(claims);
return new ClaimsPrincipal(identity);
}
}
public static IApplicationBuilder UseHeaderClaims(this IApplicationBuilder app)
{
return app.Use(next => async context =>
{
string[] claims;
if (!context.Request.Headers.TryGetValue("Jwt-Claim", out claims))
{
context.Response.StatusCode = 401;
return;
}
var dict = claims.Select(c => c.Split(Eq, 2))
.ToDictionary(a => a[0], a => a[1]);
context.User = ClaimsPrincipalBuilder.FromDictionary(dict);
await next(context);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment