Skip to content

Instantly share code, notes, and snippets.

@nikanos
Created April 18, 2023 19:07
Show Gist options
  • Save nikanos/6f2e6e6f0027caa87d4fe2f7133c4d0b to your computer and use it in GitHub Desktop.
Save nikanos/6f2e6e6f0027caa87d4fe2f7133c4d0b to your computer and use it in GitHub Desktop.
.NET Framework WebApi Bearer Token Decode
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);
}
}
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