BryanWilhite / SonghayCore

core reusable, opinionated concerns for *all* 🧐 of my C# projects
http://songhayblog.azurewebsites.net/
MIT License
1 stars 0 forks source link

add `IPrincipalExtensions` #161

Closed BryanWilhite closed 7 months ago

BryanWilhite commented 7 months ago

/// <summary>
/// Extensions of <see cref="IPrincipal"/>
/// </summary>
public static class IPrincipalExtensions
{
    /// <summary>
    /// Converts <see cref="IPrincipal"/> to a <see cref="string"/> representation of the user’s name.
    /// </summary>
    /// <param name="principal">The principal.</param>
    /// <param name="defaultUserName">Default name of the user.</param>
    /// <returns></returns>
    public static string ToUserName(this IPrincipal? principal, string defaultUserName)
    {
        var userName = principal?.Identity?.Name;

        return userName ?? defaultUserName;
    }
}