Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. {
  2. "iss": "http://oldapp.testing.com",
  3. "aud": "http://newapp.testing.com",
  4. "sub": "99239",
  5. "iat": 1425507035,
  6. "exp": 1425507065,
  7. "name": "First Last",
  8. "role": [
  9. "Admin"
  10. ]
  11.  
  12. JwtSecurityToken tokenReceived = new JwtSecurityToken(token);
  13.  
  14. JwtSecurityTokenHandler recipientTokenHandler = new JwtSecurityTokenHandler();
  15.  
  16. byte[] keyBytes = Encoding.UTF8.GetBytes("someTestSecretKeyForTestingThis");
  17. if (keyBytes.Length < 64 && tokenReceived.SignatureAlgorithm == "HS256")
  18. {
  19. Array.Resize(ref keyBytes, 64);
  20. }
  21.  
  22.  
  23. TokenValidationParameters validationParameters = new TokenValidationParameters()
  24. {
  25. ValidIssuer = "http://oldapp.testing.com",
  26. ValidAudience = "http://newapp.testing.com",
  27. IssuerSigningToken = new BinarySecretSecurityToken(keyBytes)
  28. };
  29.  
  30. try
  31. {
  32. SecurityToken validatedToken;
  33. var principal = recipientTokenHandler.ValidateToken(token, validationParameters, out validatedToken);
  34.  
  35. // Pull out the ClaimIdentity created by ValidateToken
  36. var myIdentity = principal.Identities.FirstOrDefault();
  37.  
  38. //
  39. // Copy ClaimIdentity created by the ValidateToken method and change the Authentication
  40. // type from Federated to Cookie
  41. //
  42. // Is there a better way to do this???
  43. //
  44. var ident2 = new ClaimsIdentity(myIdentity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
  45.  
  46. //
  47. // Make sure the Name claim is set correctly so that the SignIn method will work
  48. //
  49. // Why isn't the Name claim set automatically???
  50. //
  51. ident2.AddClaim(new Claim(ClaimTypes.Name, myIdentity.FindFirstValue("Name")));
  52.  
  53. // Sign the user in
  54. var ctx = Request.GetOwinContext();
  55. var authManager = ctx.Authentication;
  56. authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
  57. authManager.SignIn(ident2);
  58.  
  59. }
  60. catch (Exception ex)
  61. {
  62. System.Diagnostics.Debug.WriteLine("Exception :" + ex.Message);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement