@@ -37,30 +37,6 @@ DateTime givenTokenExpires
37
37
, givenClientSite
38
38
, givenTokenClaimName
39
39
, givenTokenExpires ) ;
40
-
41
- //var thisSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(givenAPISecret));
42
- //var thisCredentials = new SigningCredentials(thisSecurityKey, SecurityAlgorithms.HmacSha256);
43
- ////var issuer = AppConfig.Setting.JWTapiSite;
44
- ////var audience = AppConfig.Setting.JWTclientSite;
45
- ////DateTime givenTokenExpires = DateTime.UtcNow.AddMinutes(AppConfig.Setting.JWTMinutesToLive);
46
-
47
- ////Create a List of Claims, Keep claims name short
48
- //var permClaims = new List<Claim>();
49
- ////chr observation: I'm presuming that a GUID adds a bunch of randomness to the token - thus the signature won't betray the validating secret...
50
- //permClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
51
- //permClaims.Add(new Claim("valid", "1"));
52
- ////permClaims.Add(new Claim("userid", "1")); // redacted to keep api minimal
53
- //permClaims.Add(new Claim("name", givenUserID)); // our link to a user table somewhere
54
-
55
- ////Create Security Token object by giving required parameters
56
- //var token = new JwtSecurityToken(
57
- // givenIssuerSite,
58
- // givenClientSite,
59
- // permClaims,
60
- // expires: givenTokenExpires,
61
- // signingCredentials: thisCredentials);
62
- //var jwt_token = new JwtSecurityTokenHandler().WriteToken(token);
63
- //return new { data = jwt_token };
64
40
}
65
41
}
66
42
return null ;
@@ -76,9 +52,6 @@ DateTime givenTokenExpires
76
52
{
77
53
var thisSecurityKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( givenAPISecret ) ) ;
78
54
var thisCredentials = new SigningCredentials ( thisSecurityKey , SecurityAlgorithms . HmacSha256 ) ;
79
- //var issuer = AppConfig.Setting.JWTapiSite;
80
- //var audience = AppConfig.Setting.JWTclientSite;
81
- //DateTime givenTokenExpires = DateTime.UtcNow.AddMinutes(AppConfig.Setting.JWTMinutesToLive);
82
55
83
56
//Create a List of Claims, Keep claims name short
84
57
var permClaims = new List < Claim > ( ) ;
@@ -97,29 +70,8 @@ DateTime givenTokenExpires
97
70
signingCredentials : thisCredentials ) ;
98
71
var jwt_token = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
99
72
return new { data = jwt_token } ;
100
-
101
- //var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(givenSecret));
102
- //var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
103
-
104
- ////Create a List of Claims, Keep claims name short - minimalistic
105
- //var permClaims = new List<Claim>();
106
- //permClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
107
- //permClaims.Add(new Claim("valid", "1"));
108
- //permClaims.Add(new Claim("name", givenTokenClaimName)); // the key to the user db - unique
109
-
110
- ////Create Security Token object by giving required parameters
111
- //var token = new JwtSecurityToken(givenIssuerSite,
112
- // givenClientSite,
113
- // permClaims,
114
- // expires: tokenexpires,
115
- // signingCredentials: credentials);
116
- //var jwt_token = new JwtSecurityTokenHandler().WriteToken(token);
117
- //return jwt_token; // new { data = jwt_token };
118
73
}
119
- //}
120
- //public class JWTvalidator //: DelegatingHandler
121
- //{
122
-
74
+
123
75
public static bool TryRetrieveToken ( HttpRequestMessage givenRequest , out string token )
124
76
{
125
77
token = null ;
@@ -133,7 +85,7 @@ public static bool TryRetrieveToken(HttpRequestMessage givenRequest, out string
133
85
// next two lines are a kludge because the client is sending JSON instead of encoded string...
134
86
var badhead = "{\" data\" :\" " ;
135
87
if ( token . Substring ( 0 , badhead . Length ) == badhead ) { token = token . Substring ( badhead . Length ) ; token = token . Substring ( 0 , token . Length - 2 ) ; }
136
- return true ; // I was here - going to test out my JSON - then move onto bigger issues in the Claims try/catch...
88
+ return true ;
137
89
}
138
90
139
91
public bool ValidateToken ( HttpRequestMessage givenRequest , List < MockUzer > givenUsers , string givenSecret )
@@ -145,8 +97,6 @@ public bool ValidateToken(HttpRequestMessage givenRequest, List<MockUzer> givenU
145
97
true ,
146
98
true ) ;
147
99
return ValidateHeaderToken ( givenRequest , givenUsers , theseParams ) ;
148
-
149
- //return false;
150
100
}
151
101
152
102
public bool ValidateHeaderToken ( HttpRequestMessage givenRequest , List < MockUzer > givenUsers , TokenValidationParameters givenTokenValidationParameters )
@@ -196,10 +146,10 @@ bool givenIssuerSigningKey
196
146
var securityKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( givenSecret ) ) ;
197
147
TokenValidationParameters validationParameters = new TokenValidationParameters ( )
198
148
{
199
- ValidAudience = givenAudience , //"http://localhost:50191",
200
- ValidIssuer = givenIssuer , //"http://localhost:50191",
201
- ValidateLifetime = givenLifetime , // true,
202
- ValidateIssuerSigningKey = givenIssuerSigningKey , // true,
149
+ ValidAudience = givenAudience ,
150
+ ValidIssuer = givenIssuer ,
151
+ ValidateLifetime = givenLifetime ,
152
+ ValidateIssuerSigningKey = givenIssuerSigningKey ,
203
153
LifetimeValidator = this . LifetimeValidator ,
204
154
IssuerSigningKey = securityKey
205
155
} ;
@@ -223,19 +173,20 @@ public bool LifetimeValidator(DateTime? notBefore, DateTime? expires, SecurityTo
223
173
return false ;
224
174
}
225
175
226
-
227
176
public static string HashPassword ( string password , string salt )
228
177
{
229
178
using ( SHA1Managed sha1 = new SHA1Managed ( ) )
230
179
{
231
180
return Convert . ToBase64String ( sha1 . ComputeHash ( Encoding . UTF8 . GetBytes ( salt + password ) ) ) ;
232
181
}
233
182
}
234
- // cargo cult coding: https://stackoverflow.com/questions/31908529/randomnumbergenerator-proper-usage
183
+
184
+ // thanks for the reminder of how to use this: https://stackoverflow.com/questions/31908529/randomnumbergenerator-proper-usage
235
185
public static string GenRandomSeed ( )
236
186
{
237
187
return Convert . ToBase64String ( GenerateSaltNewInstance ( 42 ) ) ;
238
188
}
189
+
239
190
private static byte [ ] GenerateSaltNewInstance ( int size )
240
191
{
241
192
using ( var generator = RandomNumberGenerator . Create ( ) )
0 commit comments