Test2

Because a second test is always needed.

Posted by Nicolò Carandini on March 28, 2020 · 2 mins read

Syntax Highlighting in Jekyll

Let’s try with a little snippet:

// C# Code
public class UserInfoManager : IUserInfoManager
{
    private MyApiService myApiService;
    private UserInfo userInfo = null;

    public UserInfoManager(MyApiService myApiService)
    {
        this.myApiService = myApiService;
    }

    public void ClearUserInfo()
    {
        userInfo = null;
    }

    public async Task<UserInfo> FetchUserInfoFromApiAsync(ClaimsPrincipal user, bool ignoreCache = false, bool updateCache = true)
    {
        if (user == null)
        {
            return null;
        }

        string oId = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
        return await FetchUserInfoFromApiAsync(oId, ignoreCache, updateCache);
    }

    public async Task<UserInfo> FetchUserInfoFromApiAsync(string oId, bool ignoreCache = false, bool updateCache = true)
    {
        UserInfo result = null;

        if (!String.IsNullOrWhiteSpace(oId))
        {
            // If user info are not those of the actual logged user 
            if (userInfo != null && userInfo.AuthenticationObjectId == oId && !ignoreCache)
            {
                // return the cached user info from the private field as this is a singleton instance
                result = userInfo;
            }
            else
            {
                // If updateCache is true then nullify the cached user info as is not valid anymore
                if (updateCache)
                {
                    userInfo = null;
                }

                // Get userInfo from API
                var serviceResponse = await myApiService.GetUserInfo(oId);

                if (serviceResponse.Success)
                {
                    result = new UserInfo
                    {
                        AuthenticationObjectId = serviceResponse.Data.AuthenticationObjectId,
                        UserId = serviceResponse.Data.UserId,
                        Roles = serviceResponse.Data.Roles
                    };

                    // If updateCache is true then update the cached user info
                    // in the private field as this is a singleton instance
                    if (updateCache)
                    {
                        userInfo = result;
                    }
                }
            }
        }

        return result;
    }

    public UserInfo GetUserInfo(ClaimsPrincipal user)
    {
        if (user == null)
        {
            return null;
        }

        string oId = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
        return GetUserInfo(oId);
    }

    public UserInfo GetUserInfo(string oId)
    {
        if(!String.IsNullOrWhiteSpace(oId) && userInfo.AuthenticationObjectId == oId)
        {
            // return the cached user info from the private field as this is a singleton instance
            return userInfo;
        }

        return null;
    }
}
<AuthorizeView Policy="Logged">
    <Authorized>
        <div class="text-center mb-3">
            <h1 class="card-title">Seller App</h1>
            <p class="card-text">Ciao @context.User.Identity.Name. Con questa applicazione puoi gestire il catalogo dei tuoi prodotti.</p>
            <p>Your have these roles: @userInfo?.SpaceSeparatedRoles</p>
            <img src="/images/Logo.png" alt="Logo image" style="width:40%">
        </div>
    </Authorized>
    <NotAuthorized>
        <div class="text-center mb-3">
            <h1 class="card-title">Seller App</h1>
            <p class="card-text">Questa applicazione è riservata ai venditori. Effettua il login per accedere.</p>
            <img src="/images/Logo.png" alt="Logo image" style="width:40%">
        </div>
    </NotAuthorized>
</AuthorizeView>