skip to content

A C# role check written as role.ToUpper() == "ADMIN" passes in tests but fails on servers whose culture is tr-TR — why, and how do you fix it?

level: seniorimportance: should knowfreq 45%

answer

  1. four letters i in Turkish
  2. ToUpper reads the current culture
  3. dotted capital İ
  4. invariant casing or ordinal ignore-case

basics

~10 s

String.ToUpper() uses the current culture, and Turkish uppercases i to the dotted İ (U+0130), so "admin" becomes "ADMİN". Compare with string.Equals(role, "admin", StringComparison.OrdinalIgnoreCase) or use ToUpperInvariant().

solid answer

~40 s

`ToUpper()` with no argument is `ToUpper(CultureInfo.CurrentCulture)`. Turkish (and Azerbaijani) have two distinct i letters: dotted `i` uppercases to `İ` (U+0130) and dotless `ı` (U+0131) uppercases to `I`. So on a `tr-TR` server `"admin".ToUpper()` returns `"ADMİN"`, which is not `"ADMIN"`, and the check fails; tests passed because the test machine ran under `en-US`. `CurrentCultureIgnoreCase` fails the same way, for the same reason. A role name is an identifier, so compare it ordinally: `string.Equals(role, "admin", StringComparison.OrdinalIgnoreCase)`, which neither allocates nor reads the culture. If you need a normalized copy for storage, use `ToUpperInvariant()`. Then reproduce the bug in a test by setting `CultureInfo.CurrentCulture = new CultureInfo("tr-TR")`, and turn on CA1311 so a parameterless `ToUpper()` or `ToLower()` cannot creep back.

code

csharp · 15 lines
csharp
using System.Globalization;

string role = "admin";

foreach (var name in new[] { "en-US", "tr-TR" })
{
    CultureInfo.CurrentCulture = new CultureInfo(name);
    Console.WriteLine($"{name}: ToUpper()={role.ToUpper()} " +
        $"match={role.ToUpper() == "ADMIN"} " +
        $"cultureIgnoreCase={string.Equals(role, "ADMIN", StringComparison.CurrentCultureIgnoreCase)} " +
        $"invariant={role.ToUpperInvariant() == "ADMIN"} " +
        $"ordinalIgnoreCase={string.Equals(role, "ADMIN", StringComparison.OrdinalIgnoreCase)}");
}

Console.WriteLine("FILE".ToLower(new CultureInfo("tr-TR")));

go deeper

for a junior

Recall that ToUpper() and ToLower() use the current culture, and that Turkish has a dotted capital İ and a dotless small ı.

for a middle

Explain why both ToUpper() == and CurrentCultureIgnoreCase fail under tr-TR, and why OrdinalIgnoreCase or ToUpperInvariant() does not.

for a senior

Diagnose it from region-specific failures, name the security angle of culture-aware filters, and reproduce it in a test by setting CultureInfo.CurrentCulture.

for a principal

Turn the incident into prevention: CA1311 and CA1304 in the build, and an ordinal-by-default rule for identifiers across services.

## What happens `string.ToUpper()` has no culture parameter, so it uses `CultureInfo.CurrentCulture`; in the reference source it is `ToUpper(null)`, and a null culture becomes the current one. The current culture comes from the machine or the user account unless the program sets it, so the same code can uppercase differently on a laptop, in CI and on a server in another region. Most Latin-script languages have one letter i with one capital, I. **Turkish and Azerbaijani have two pairs**: | Letter | Lowercase | Uppercase | |---|---|---| | dotted i | `i` (U+0069) | `İ` (U+0130) | | dotless i | `ı` (U+0131) | `I` (U+0049) | Under `tr-TR`, `"admin".ToUpper()` returns `"ADMİN"`, and `"ADMİN" == "ADMIN"` is false, because `==` compares code units and U+0130 is not U+0049. The reverse direction breaks too: `"FILE".ToLower(new CultureInfo("tr-TR"))` returns `"fıle"`, and `"LIST.HTML".ToLower()` under `tr-TR` gives `"lıst.html"`, so lowercased file extensions or header names stop matching. ## Why the obvious alternatives fail too - **`string.Equals(role, "ADMIN", StringComparison.CurrentCultureIgnoreCase)`** also returns false under `tr-TR`. "Ignore case" means "ignore case as this culture defines it", and in Turkish `i` and `I` are not case variants of one letter. - **`role.ToLower() == "admin"`** fails whenever the input contains a capital I, since `I` lowercases to `ı`. - **Hard-coding a culture such as `en-US`** works but states the wrong intent; a role name has no language. ## The fix A role name, a username, a scheme or a file extension is a **non-linguistic identifier**. Compare it ordinally: ```csharp bool isAdmin = string.Equals(role, "admin", StringComparison.OrdinalIgnoreCase); ``` This is correct on every server, allocates nothing and documents intent. If you must produce a normalized copy (for a cache key or a database column), call **`ToUpperInvariant()`**: the .NET guidance recommends it over `ToLowerInvariant()` for normalization, and `OrdinalIgnoreCase` behaves like `ToUpperInvariant()` on both sides followed by an ordinal comparison. Note what `OrdinalIgnoreCase` does with Turkish letters: it uses the invariant casing table, which does not pair `ı` with `I` or `İ` with `i`. For identifiers that is fine and predictable. For words a Turkish user types into a search box, ordinal is the wrong tool; compare with that user's `CultureInfo`. ## Why it matters beyond a failed login The .NET documentation's own example is security-shaped: a check that blocks URIs starting with `"FILE:"` using a culture-aware case-insensitive comparison returns false for `"file:"` under Turkish rules, so a filter can be bypassed. The same class of bug shows up when: 1. keys are normalized with `ToUpper()` on one server and looked up on another with a different culture; 2. a configuration flag such as `"Enabled"` is compared after `ToLower()`; 3. a whitelist of file extensions such as `".ini"` or `".gif"` is lowercased before matching. Since .NET 5, globalization uses ICU on Windows as well as on Linux and macOS (falling back to the older NLS only where ICU cannot be loaded), so the same results appear on Linux containers and on current Windows hosts. ## Reproducing and preventing it - **Write the failing test first.** Set `CultureInfo.CurrentCulture = new CultureInfo("tr-TR")` at the start of the test (and restore it afterwards); the check fails without any server. - **Turn on the analyzers.** CA1311 ("Specify a culture or use an invariant version") flags parameterless `ToUpper()` and `ToLower()`, and CA1304 ("Specify CultureInfo") fires on the same calls. Both are off by default in .NET 10. - **Search the codebase** for `.ToUpper()` and `.ToLower()` used inside comparisons, and replace each with an explicit `StringComparison`. - **Look past casing.** `CurrentCultureIgnoreCase` in an identifier check, a `StringComparer.CurrentCultureIgnoreCase` dictionary of user names, and `StartsWith(string)` on a URL scheme all carry the same culture dependency without any `ToUpper()` in sight. - **Remember Azerbaijani.** The .NET docs note that `az` cultures share the Turkish i rule; an ordinal fix covers both, while a fix that special-cases `tr-TR` does not. The diagnosis has one right answer once you know Turkish casing; the skill is recognising it from "works on my machine, fails in one region".

  • Why does string.Equals(role, "ADMIN", StringComparison.CurrentCultureIgnoreCase) also return false under tr-TR?
    Because ignoring case follows the culture's own case pairs. In Turkish, `i` pairs with `İ` and `ı` pairs with `I`, so `i` and `I` are different letters rather than two cases of one letter. Only an ordinal or invariant comparison treats them as a case pair.
  • How do you reproduce this bug in a unit test on a machine set to English?
    Set `CultureInfo.CurrentCulture = new CultureInfo("tr-TR")` before the code under test runs and restore the previous culture afterwards. With ICU culture data, `"admin".ToUpper()` then returns `"ADMİN"` exactly as on the Turkish server, so the assertion fails locally.
  • Should you fix it with ToUpperInvariant() or with StringComparison.OrdinalIgnoreCase?
    For a comparison, `OrdinalIgnoreCase`: it gives the same answer as upper-casing both sides with the invariant culture but allocates nothing and states the intent. Use `ToUpperInvariant()` only when you need a normalized string to store or to use as a key.

  • ToUpper() always maps i to I, whatever the culture.
  • CurrentCultureIgnoreCase fixes the Turkish problem because it ignores case.
  • This only happens on Windows, so Linux containers are safe.
  • Lowercasing with ToLower() instead of ToUpper() avoids the issue.
  • OrdinalIgnoreCase treats the Turkish dotless ı as the lowercase of I.