Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/JwtAuthForWebAPI.SampleSite/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public static void Register(HttpConfiguration config)
var tokenBuilder = new SecurityTokenBuilder();
var configReader = new ConfigurationReader();

var jwtHandlerCert = new JwtAuthenticationMessageHandler
var jwtHandlerCert = new JwtAuthenticationMessageHandler(Logger.Instance)
{
AllowedAudience = configReader.AllowedAudience,
AllowedAudiences = configReader.AllowedAudiences,
Issuer = configReader.Issuer,
SigningToken = tokenBuilder.CreateFromCertificate(configReader.SubjectCertificateName),
PrincipalTransformer = new SamplePrincipalTransformer()
};
var jwtHandlerSharedKey = new JwtAuthenticationMessageHandler
};

var jwtHandlerSharedKey = new JwtAuthenticationMessageHandler(Logger.Instance)
{
AllowedAudience = configReader.AllowedAudience,
Issuer = configReader.Issuer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Logger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\SamplePrincipal.cs" />
<Compile Include="Security\SamplePrincipalTransformer.cs" />
Expand Down
44 changes: 44 additions & 0 deletions src/JwtAuthForWebAPI.SampleSite/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


using System;
using log4net;

namespace JwtAuthForWebAPI.SampleSite
{
public class Logger : ILogger
{
readonly ILog _logger = LogManager.GetLogger("JwtAuthForWebAPI");

private static Logger instance;

private Logger() { }

public static Logger Instance
{
get
{
if (instance == null)
{
instance = new Logger();
}
return instance;
}
}

public void DebugFormat(string message, params object[] paramList)
{
_logger.DebugFormat(message,paramList);
}

public void WarnFormat(string message, Exception ex)
{
_logger.WarnFormat(message, ex);
}

public void ErrorFormat(string message, Exception ex)
{
_logger.WarnFormat(message, ex);
}
}
}

35 changes: 35 additions & 0 deletions src/JwtAuthForWebAPI/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


using System;
using System.Diagnostics;

namespace JwtAuthForWebAPI
{
public interface ILogger
{
void DebugFormat(string message, params object[] paramList);

void WarnFormat(string message, Exception ex);

void ErrorFormat(string message, Exception ex);
}

public class DefaultLogger : ILogger
{
public void DebugFormat(string message, params object[] paramList)
{
Trace.TraceInformation(message,paramList);
}

public void WarnFormat(string message, Exception ex)
{
Trace.TraceWarning(message, ex.Message);
}

public void ErrorFormat(string message, Exception ex)
{
Trace.TraceError(message, ex.Message);
}
}
}

4 changes: 1 addition & 3 deletions src/JwtAuthForWebAPI/JwtAuthForWebAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\..\lib\log4net.2.0.2\lib\net40-full\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand All @@ -53,6 +50,7 @@
<Compile Include="ConfigurationReader.cs" />
<Compile Include="IJwtSecurityToken.cs" />
<Compile Include="IJwtSecurityTokenHandler.cs" />
<Compile Include="ILogger.cs" />
<Compile Include="IPrincipalTransformer.cs" />
<Compile Include="JwtAuthenticationMessageHandler.cs" />
<Compile Include="JwtAuthForWebAPIConfigurationSection.cs" />
Expand Down
10 changes: 7 additions & 3 deletions src/JwtAuthForWebAPI/JwtAuthenticationMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using log4net;

namespace JwtAuthForWebAPI
{
Expand All @@ -21,13 +20,18 @@ namespace JwtAuthForWebAPI
/// </summary>
public class JwtAuthenticationMessageHandler : DelegatingHandler
{
private readonly ILog _logger = LogManager.GetLogger("JwtAuthForWebAPI");
private readonly ILogger _logger = new DefaultLogger();

/// <summary>
/// String representation of the Bearer scheme, used for JWTs.
/// </summary>
public const string BearerScheme = "Bearer";

public JwtAuthenticationMessageHandler(ILogger logger) : base()
{
_logger = logger;
}

public JwtAuthenticationMessageHandler()
{
AllowedAudience = "http://www.example.com";
Expand Down Expand Up @@ -130,7 +134,7 @@ protected override Task<HttpResponseMessage> SendAsync(

if (string.IsNullOrEmpty(tokenString))
{
_logger.Debug("Token not found in authorization header or request cookie");
_logger.DebugFormat("Token not found in authorization header or request cookie");
return BaseSendAsync(request, cancellationToken);
}

Expand Down