Created
September 22, 2015 16:26
-
-
Save ThatRendle/546141740f244dcfba88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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