locked
case sensitivity in c# RRS feed

  • Question

  • User-973886032 posted

    Hello,

    how do i turn off case sensitivity with usernames and password authentication in c# ?

    Also, when i  run signup forms to check if a username, email or phone number exists ?

    thanks

    Ehi

    Tuesday, July 15, 2008 4:29 PM

Answers

  • User1191518856 posted

    Do you really want to turn off case sensitivity for passwords?

    As you've already noticed, string comparison is performed with regards to casing by default. But this can be circumvented if you use the following construct:

    if (string1.Equals(string2, StringComparison.OrdinalIgnoreCase))

    If you're doing the comparison in SQL (Server), it is already case-insensitive by default.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2008 8:08 AM
  • User-396029291 posted

    The recommendation for string comparison has been updated for Whidbey (Visual Studio 2005) and there is an excellent MSDN article on this. One of the highlights is the introduction of the clear cut enumeration that can be passed into most string comparison methods to indicate the kind of comparison you are trying to make.

    [Serializable]

    [ComVisible(true)]

    public enum StringComparison{

    CurrentCulture = 0,

    CurrentCultureIgnoreCase = 1,

     InvariantCulture = 2,

     InvariantCultureIgnoreCase = 3,

    Ordinal = 4,

     OrdinalIgnoreCase = 5,

    }

    The recommendation also states that for culture-agnostic comparisons use the Ordinal and OrdinalIgnoreCase comparisons.

    These are fast and also safe. They rely on byte matching and are excellent options for matching strings for internal (non-UI) processing.

    string.Compare(str1, str2, StringComparison.Ordinal);

    With the introduction of the guidelines, developers have become defensive and have started looking for all code that compare string and re-coding them to meet the guidelines.

    Let's take a look at the most common culture-agnostic string matching used in code and see if they are safe.

    string.Equals(string1, string2)

    Default interpretation for equals is Ordinal so using this is fine.

    In case of using any other type of comparison use in the lines of string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2008 8:40 AM

All replies

  • User1191518856 posted

    Do you really want to turn off case sensitivity for passwords?

    As you've already noticed, string comparison is performed with regards to casing by default. But this can be circumvented if you use the following construct:

    if (string1.Equals(string2, StringComparison.OrdinalIgnoreCase))

    If you're doing the comparison in SQL (Server), it is already case-insensitive by default.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2008 8:08 AM
  • User-396029291 posted

    The recommendation for string comparison has been updated for Whidbey (Visual Studio 2005) and there is an excellent MSDN article on this. One of the highlights is the introduction of the clear cut enumeration that can be passed into most string comparison methods to indicate the kind of comparison you are trying to make.

    [Serializable]

    [ComVisible(true)]

    public enum StringComparison{

    CurrentCulture = 0,

    CurrentCultureIgnoreCase = 1,

     InvariantCulture = 2,

     InvariantCultureIgnoreCase = 3,

    Ordinal = 4,

     OrdinalIgnoreCase = 5,

    }

    The recommendation also states that for culture-agnostic comparisons use the Ordinal and OrdinalIgnoreCase comparisons.

    These are fast and also safe. They rely on byte matching and are excellent options for matching strings for internal (non-UI) processing.

    string.Compare(str1, str2, StringComparison.Ordinal);

    With the introduction of the guidelines, developers have become defensive and have started looking for all code that compare string and re-coding them to meet the guidelines.

    Let's take a look at the most common culture-agnostic string matching used in code and see if they are safe.

    string.Equals(string1, string2)

    Default interpretation for equals is Ordinal so using this is fine.

    In case of using any other type of comparison use in the lines of string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2008 8:40 AM