Created
April 18, 2023 19:07
-
-
Save nikanos/6f2e6e6f0027caa87d4fe2f7133c4d0b to your computer and use it in GitHub Desktop.
.NET Framework WebApi Bearer Token Decode
This file contains 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 MachineKeyProtector : IDataProtector | |
{ | |
private readonly string[] _purpose = | |
{ | |
typeof(OAuthAuthorizationServerMiddleware).Namespace, | |
"Access_Token", | |
"v1" | |
}; | |
public byte[] Protect(byte[] userData) | |
{ | |
throw new NotImplementedException(); | |
} | |
public byte[] Unprotect(byte[] protectedData) | |
{ | |
return System.Web.Security.MachineKey.Unprotect(protectedData, _purpose); | |
} | |
} |
This file contains 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 TokenUtils | |
{ | |
public static AuthenticationTicket DecodeBearerToken(string token) | |
{ | |
var secureDataFormat = new TicketDataFormat(new MachineKeyProtector()); | |
AuthenticationTicket ticket = secureDataFormat.Unprotect(token); | |
return ticket; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment