0
votes

I am using ASP.NET identity in MVC 5 and wondered the best way to perform string comparison during say, a query to a table where records have been entered with the userId as an foreign key.

Normally I would just plump for == but I wasn't sure how the string for a userId inside .NET identity is created. I guess it's actually a GUID of sorts under the hood so I'd presume that == would be fine but I wondered if the culture or case would come into it at all.

Does anyone know if the string is a unique GUID for == type comparison, or should I be using string.Equals and some StringComparison option to account for cultures?

2
Identity uses strings for user Id by default, have you changed it? - DavidG
I've not changed anything, it's a string as I specified - dougajmcdonald

2 Answers

0
votes

ASP.NET Identity can use GUID for keys. In that case GUID should not have any culture specific characters. So StringComparision. InvariantCultureIgnoreCase should go for string.Equals in C#. ToLower (ToUpper) can be used for SQL queries (LINQ).

0
votes

You probably want to use Ordinal/OrdinalIgnoreCase StringComparisons here as it compares strings character by character without regard to linguistic convention.

Read more here: https://msdn.microsoft.com/en-us/library/cc165449.aspx

And here: https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx

Code samples:

string1.Equals(string2, StringComparison.Ordinal);
string1.Equals(string2, StringComparison.OrdinalIgnoreCase);