Add fallback logging for username in AuthZ

This commit is contained in:
Hao Kung 2016-03-31 13:09:32 -07:00
parent 16a0482238
commit 4d6ad51f8a
1 changed files with 25 additions and 2 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
@ -51,16 +52,38 @@ namespace Microsoft.AspNetCore.Authorization
if (authContext.HasSucceeded)
{
_logger.UserAuthorizationSucceeded(user?.Identity?.Name);
_logger.UserAuthorizationSucceeded(GetUserNameForLogging(user));
return true;
}
else
{
_logger.UserAuthorizationFailed(user?.Identity?.Name);
_logger.UserAuthorizationFailed(GetUserNameForLogging(user));
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)
{
if (policyName == null)