Add fallback logging for username in AuthZ
This commit is contained in:
parent
16a0482238
commit
4d6ad51f8a
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using System.Security.Principal;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
|
@ -51,16 +52,38 @@ namespace Microsoft.AspNetCore.Authorization
|
||||||
|
|
||||||
if (authContext.HasSucceeded)
|
if (authContext.HasSucceeded)
|
||||||
{
|
{
|
||||||
_logger.UserAuthorizationSucceeded(user?.Identity?.Name);
|
_logger.UserAuthorizationSucceeded(GetUserNameForLogging(user));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.UserAuthorizationFailed(user?.Identity?.Name);
|
_logger.UserAuthorizationFailed(GetUserNameForLogging(user));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetUserNameForLogging(ClaimsPrincipal user)
|
||||||
|
{
|
||||||
|
var identity = user?.Identity;
|
||||||
|
if (identity != null)
|
||||||
|
{
|
||||||
|
var name = identity.Name;
|
||||||
|
if (name != null)
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
return GetClaimValue(identity, "sub")
|
||||||
|
?? GetClaimValue(identity, ClaimTypes.Name)
|
||||||
|
?? GetClaimValue(identity, ClaimTypes.NameIdentifier);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetClaimValue(IIdentity identity, string claimsType)
|
||||||
|
{
|
||||||
|
return (identity as ClaimsIdentity)?.FindFirst(claimsType)?.Value;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
|
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
|
||||||
{
|
{
|
||||||
if (policyName == null)
|
if (policyName == null)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue