Bläddra i källkod

[Authentication] Token validation helper

develop
Robin Thoni 9 år sedan
förälder
incheckning
ca1c02dcdd

+ 26
- 0
Authentication/Business/JWT/TokenManager.cs Visa fil

@@ -2,6 +2,7 @@
2 2
 using System.Configuration;
3 3
 using System.IdentityModel.Tokens;
4 4
 using System.Security.Claims;
5
+using System.ServiceModel.Security.Tokens;
5 6
 
6 7
 namespace iiie.Authentication.Business.JWT
7 8
 {
@@ -47,5 +48,30 @@ namespace iiie.Authentication.Business.JWT
47 48
             JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
48 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 Visa fil

@@ -62,27 +62,7 @@ namespace iiie.Authentication.Business.JWT
62 62
 
63 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 66
                 Thread.CurrentPrincipal = claim;
87 67
 
88 68
                 var name = ((ClaimsIdentity)claim.Identity).Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name);

Laddar…
Avbryt
Spara