Skip to content

Commit

Permalink
feat: log user name
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 5, 2021
1 parent 93b9b19 commit 32fabc2
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
using Aguacongas.IdentityServer.Store;
using Aguacongas.IdentityServer.Store.Entity;
using IdentityModel;
using IdentityServer4.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
Expand Down Expand Up @@ -109,15 +112,20 @@ private static async Task Authenticate(HttpContext context, Func<Task> next, str
return;
}

var logger = context.RequestServices.GetRequiredService<ILogger<HttpContext>>();
if (context.User.Identity.IsAuthenticated)
{
using var authscope = logger.BeginScope(new Dictionary<string, object> { ["User"] = context.User.GetDisplayName() });
await next().ConfigureAwait(false);
return;
}

var result = await context.AuthenticateAsync(authicationScheme)
.ConfigureAwait(false);

context.User = result.Principal;
using var scope = logger.BeginScope(new Dictionary<string, object> { ["User"] = context.User.GetDisplayName() });

if (!result.Succeeded &&
path.StartsWithSegments("/register", StringComparison.OrdinalIgnoreCase) &&
request.Method != HttpMethods.Post)
Expand All @@ -126,11 +134,8 @@ private static async Task Authenticate(HttpContext context, Func<Task> next, str
return;
}

context.User = result.Principal;

await next().ConfigureAwait(false);


}

private static async Task SetForbiddenResponse(HttpContext context)
Expand Down

0 comments on commit 32fabc2

Please sign in to comment.