Name of a Role with accent in the Identity database

Asked

Viewed 129 times

1

The need to create a Role with emphasis on the table of Roles of Identity in the database: "Technical".

inserir a descrição da imagem aqui

My question is whether this can generate further problems, perhaps at the time of checking something like User.IsInRole("Técnico") or if the database may get lost, do not know...

I wonder if I can create without fear of facing future problems and whether it is recommended to use accents for this type of information or maintain without even accentuation.

1 answer

3


There is no problem técnico in using accents, or even spaces. Until why what is compared is not the Name and yes the NormalizedName, this by definition must allow comparison with special characters.: Unicode® Standard Annex #15

Let’s take a look at the source code Identity:

Rolestore.Cs

    /// <summary>
    /// Finds the role who has the specified normalized name as an asynchronous operation.
    /// </summary>
    /// <param name="normalizedName">The normalized role name to look for.</param>
    /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
    /// <returns>A <see cref="Task{TResult}"/> that result of the look up.</returns>
    public virtual Task<TRole> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
    {
        cancellationToken.ThrowIfCancellationRequested();
        ThrowIfDisposed();
        return Roles.FirstOrDefaultAsync(r => r.NormalizedName == normalizedName, cancellationToken);
    }

Note that the property that is used to find a Scroll is the NormalizedName

Now let’s look at the method you should be using to create a Role:

Rolemanager.Cs

    /// <summary>
    /// Creates the specified <paramref name="role"/> in the persistence store.
    /// </summary>
    /// <param name="role">The role to create.</param>
    /// <returns>
    /// The <see cref="Task"/> that represents the asynchronous operation.
    /// </returns>
    public virtual async Task<IdentityResult> CreateAsync(TRole role)
    {
        ThrowIfDisposed();
        if (role == null)
        {
            throw new ArgumentNullException(nameof(role));
        }
        var result = await ValidateRoleAsync(role);
        if (!result.Succeeded)
        {
            return result;
        }
        await UpdateNormalizedRoleNameAsync(role);
        result = await Store.CreateAsync(role, CancellationToken);
        return result;
    }

    /// <summary>
    /// Updates the normalized name for the specified <paramref name="role"/>.
    /// </summary>
    /// <param name="role">The role whose normalized name needs to be updated.</param>
    /// <returns>
    /// The <see cref="Task"/> that represents the asynchronous operation.
    /// </returns>
    public virtual async Task UpdateNormalizedRoleNameAsync(TRole role)
    {
        var name = await GetRoleNameAsync(role);
        await Store.SetNormalizedRoleNameAsync(role, NormalizeKey(name), CancellationToken);
    }

    /// <summary>
    /// Gets a normalized representation of the specified <paramref name="key"/>.
    /// </summary>
    /// <param name="key">The value to normalize.</param>
    /// <returns>A normalized representation of the specified <paramref name="key"/>.</returns>
    public virtual string NormalizeKey(string key)
    {
        return (KeyNormalizer == null) ? key : KeyNormalizer.Normalize(key);
    }

Lookupnormalizer.Cs

    public class LookupNormalizer : ILookupNormalizer
    {
        public string Normalize(string key)
        {
            return key.Normalize().ToLowerInvariant();
        }
    }
  • Very interesting, +1, but an addendum, and in the versions prior to .Net Core I didn’t have the field NormalizedName!?

  • 1

    @Barbetta without performing some tests, or even without looking at the source code of it, is difficult to give you an answer. But I believe it should also work (EF6 should be mature enough to know in which situation should normalize the name of Role before performing the comparison).

  • I did a quick test here with accents and spaces and it was normal ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.