Skip to content

Commit 0af9927

Browse files
TraceExceptionLogger and Jwt improvement
1 parent 24d7423 commit 0af9927

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

Utils/JwtManager.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
using System;
1+
using Microsoft.IdentityModel.Tokens;
2+
using System;
23
using System.IdentityModel.Tokens.Jwt;
4+
using System.Runtime.Caching;
35
using System.Security.Claims;
4-
using Microsoft.IdentityModel.Tokens;
6+
using System.Security.Cryptography;
57

68
namespace Utils
79
{
810
public static class JwtManager
911
{
10-
/// <summary>
11-
/// Use the below code to generate symmetric Secret Key
12-
/// var hmac = new HMACSHA256();
13-
/// var key = Convert.ToBase64String(hmac.Key);
14-
/// </summary>
15-
private const string Secret = "db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw==";
12+
private static MemoryCache cache;
1613

1714
public static string GenerateToken(string username, int expireMinutes = 20)
1815
{
19-
var symmetricKey = Convert.FromBase64String(Secret);
16+
cache = new MemoryCache("CachingProvider");
17+
18+
var hmac = new HMACSHA256();
19+
var key = Convert.ToBase64String(hmac.Key);
20+
21+
var symmetricKey = Convert.FromBase64String(key);
2022
var tokenHandler = new JwtSecurityTokenHandler();
2123

2224
var now = DateTime.UtcNow;
@@ -35,11 +37,15 @@ public static string GenerateToken(string username, int expireMinutes = 20)
3537
var stoken = tokenHandler.CreateToken(tokenDescriptor);
3638
var token = tokenHandler.WriteToken(stoken);
3739

40+
cache.Add(token, key, DateTimeOffset.MaxValue);
41+
3842
return token;
3943
}
4044

4145
public static ClaimsPrincipal GetPrincipal(string token)
4246
{
47+
var key = cache[token].ToString();
48+
4349
try
4450
{
4551
var tokenHandler = new JwtSecurityTokenHandler();
@@ -48,7 +54,7 @@ public static ClaimsPrincipal GetPrincipal(string token)
4854
if (jwtToken == null)
4955
return null;
5056

51-
var symmetricKey = Convert.FromBase64String(Secret);
57+
var symmetricKey = Convert.FromBase64String(key);
5258

5359
var validationParameters = new TokenValidationParameters()
5460
{

Utils/Utils.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
4545
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.5.1.4\lib\net451\System.IdentityModel.Tokens.Jwt.dll</HintPath>
4646
</Reference>
47+
<Reference Include="System.Runtime.Caching" />
4748
<Reference Include="System.XML" />
4849
</ItemGroup>
4950
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Diagnostics;
2+
using System.Web.Http.ExceptionHandling;
3+
4+
namespace WebAPI.Helpers
5+
{
6+
public class TraceExceptionLogger : ExceptionLogger
7+
{
8+
public override void Log(ExceptionLoggerContext context)
9+
{
10+
Trace.TraceError(context.ExceptionContext.Exception.ToString());
11+
// Log your error here, Db or Service.
12+
}
13+
}
14+
}

WebAPI/WebAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
<Compile Include="Global.asax.cs">
202202
<DependentUpon>Global.asax</DependentUpon>
203203
</Compile>
204+
<Compile Include="Helpers\TraceExceptionLogger.cs" />
204205
<Compile Include="Properties\AssemblyInfo.cs" />
205206
<Compile Include="Requests\UserLoginExample.cs" />
206207
<Compile Include="Requests\UserLogin.cs" />

0 commit comments

Comments
 (0)