Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "iss": "http://oldapp.testing.com",
- "aud": "http://newapp.testing.com",
- "sub": "99239",
- "iat": 1425507035,
- "exp": 1425507065,
- "name": "First Last",
- "role": [
- "Admin"
- ]
- JwtSecurityToken tokenReceived = new JwtSecurityToken(token);
- JwtSecurityTokenHandler recipientTokenHandler = new JwtSecurityTokenHandler();
- byte[] keyBytes = Encoding.UTF8.GetBytes("someTestSecretKeyForTestingThis");
- if (keyBytes.Length < 64 && tokenReceived.SignatureAlgorithm == "HS256")
- {
- Array.Resize(ref keyBytes, 64);
- }
- TokenValidationParameters validationParameters = new TokenValidationParameters()
- {
- ValidIssuer = "http://oldapp.testing.com",
- ValidAudience = "http://newapp.testing.com",
- IssuerSigningToken = new BinarySecretSecurityToken(keyBytes)
- };
- try
- {
- SecurityToken validatedToken;
- var principal = recipientTokenHandler.ValidateToken(token, validationParameters, out validatedToken);
- // Pull out the ClaimIdentity created by ValidateToken
- var myIdentity = principal.Identities.FirstOrDefault();
- //
- // Copy ClaimIdentity created by the ValidateToken method and change the Authentication
- // type from Federated to Cookie
- //
- // Is there a better way to do this???
- //
- var ident2 = new ClaimsIdentity(myIdentity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
- //
- // Make sure the Name claim is set correctly so that the SignIn method will work
- //
- // Why isn't the Name claim set automatically???
- //
- ident2.AddClaim(new Claim(ClaimTypes.Name, myIdentity.FindFirstValue("Name")));
- // Sign the user in
- var ctx = Request.GetOwinContext();
- var authManager = ctx.Authentication;
- authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
- authManager.SignIn(ident2);
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine("Exception :" + ex.Message);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement