Browse Source

[Authentication] Token validation helper

develop
Robin Thoni 9 years ago
parent
commit
ca1c02dcdd

+ 26
- 0
Authentication/Business/JWT/TokenManager.cs View File

2
 using System.Configuration;
2
 using System.Configuration;
3
 using System.IdentityModel.Tokens;
3
 using System.IdentityModel.Tokens;
4
 using System.Security.Claims;
4
 using System.Security.Claims;
5
+using System.ServiceModel.Security.Tokens;
5
 
6
 
6
 namespace iiie.Authentication.Business.JWT
7
 namespace iiie.Authentication.Business.JWT
7
 {
8
 {
47
             JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
48
             JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
48
             return jh.WriteToken(jst);
49
             return jh.WriteToken(jst);
49
         }
50
         }
51
+
52
+        public static ClaimsPrincipal ParseToken(string token)
53
+        {
54
+            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler
55
+            {
56
+                Configuration = new SecurityTokenHandlerConfiguration()
57
+                {
58
+                    MaxClockSkew = new TimeSpan(0, 1, 0)
59
+                }
60
+            };
61
+
62
+            var stringValidator = ConfigurationManager.AppSettings["StringValidator"];
63
+            TokenValidationParameters validationParameters = new TokenValidationParameters()
64
+            {
65
+                RequireSignedTokens = true,
66
+                RequireExpirationTime = true,
67
+                ValidAudience = stringValidator,
68
+                ValidateIssuerSigningKey = true,
69
+                ValidIssuer = "urn:" + stringValidator,
70
+                IssuerSigningToken = new BinarySecretSecurityToken(Convert.FromBase64String(ConfigurationManager.AppSettings["CredentialKey"]))
71
+            };
72
+
73
+            SecurityToken validateToken;
74
+            return tokenHandler.ValidateToken(token, validationParameters, out validateToken);
75
+        }
50
     }
76
     }
51
 }
77
 }

+ 1
- 21
Authentication/Business/JWT/TokenValidationHandler.cs View File

62
 
62
 
63
             try
63
             try
64
             {
64
             {
65
-                JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler
66
-                {
67
-                    Configuration = new SecurityTokenHandlerConfiguration()
68
-                    {
69
-                        MaxClockSkew = new TimeSpan(0, 1, 0)
70
-                    }
71
-                };
72
-
73
-                var stringValidator = ConfigurationManager.AppSettings["StringValidator"];
74
-                TokenValidationParameters validationParameters = new TokenValidationParameters()
75
-                {
76
-                    RequireSignedTokens = true,
77
-                    RequireExpirationTime = true,
78
-                    ValidAudience = stringValidator,
79
-                    ValidateIssuerSigningKey = true,
80
-                    ValidIssuer = "urn:" + stringValidator,
81
-                    IssuerSigningToken = new BinarySecretSecurityToken(Convert.FromBase64String(ConfigurationManager.AppSettings["CredentialKey"]))
82
-                };
83
-
84
-                SecurityToken validateToken;
85
-                ClaimsPrincipal claim = tokenHandler.ValidateToken(token, validationParameters, out validateToken);
65
+                var claim = TokenManager.ParseToken(token);
86
                 Thread.CurrentPrincipal = claim;
66
                 Thread.CurrentPrincipal = claim;
87
 
67
 
88
                 var name = ((ClaimsIdentity)claim.Identity).Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name);
68
                 var name = ((ClaimsIdentity)claim.Identity).Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name);

Loading…
Cancel
Save