Created
March 4, 2017 03:18
-
-
Save yzraeu/6b749a392ba5117014a14b20b19ac7cf to your computer and use it in GitHub Desktop.
Identity Extensions, reading the JWT token from Firebase
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 class IdentityExtensions | |
| { | |
| public static string GetEmail(this ClaimsPrincipal current) | |
| { | |
| return GetClaimValue(current, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"); | |
| } | |
| public static string GetName(this ClaimsPrincipal current) | |
| { | |
| return GetClaimValue(current, "name"); | |
| } | |
| public static string GetUid(this ClaimsPrincipal current) | |
| { | |
| return GetClaimValue(current, "user_id"); | |
| } | |
| public static string GetProfilePicture(this ClaimsPrincipal current) | |
| { | |
| return GetClaimValue(current, "picture"); | |
| } | |
| public static DateTime GetExpirationDateTime(this ClaimsPrincipal current) | |
| { | |
| var timestamp = long.Parse(GetClaimValue(current, "exp")); | |
| var datetime = DateTimeOffset.FromUnixTimeSeconds(timestamp).UtcDateTime; | |
| return datetime; | |
| } | |
| private static string GetClaimValue(ClaimsPrincipal principal, string type) | |
| { | |
| return principal.Claims.FirstOrDefault(c => c.Type.Equals(type)).Value; | |
| } | |
| } |
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 TestController : Controller | |
| { | |
| [HttpGet] | |
| public IActionResult Get() | |
| { | |
| var email = HttpContext.User.GetEmail(); | |
| var name = HttpContext.User.GetName(); | |
| var uid = HttpContext.User.GetUid(); | |
| var profile = HttpContext.User.GetProfilePicture(); | |
| var expiration = HttpContext.User.GetExpirationDateTime(); | |
| return Ok(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment