Reputation: 573
I have an example username:
corp\myusername
So if I put this in C# it wants to escaoe it with "\" so it turns out to be:
corp\\myusername but I need to to be corp\myusername. Any ideas how to get it to work?
Thanks much
Upvotes: 0
Views: 1539
Reputation: 18980
I imagine you're trying to separate the domain from the user name, to get the username for your application when using Windows authentication in your site, yes? I also ran into the issue where some machines would return the value as upper case, and some as lower case, so be careful to force the case in your source code (or database)!
string windowsLogin = HttpContext.Current.User.Identity.Name;
int hasDomain = windowsLogin.IndexOf(@"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
}
return windowsLogin.ToLower();
Upvotes: 0
Reputation:
Use a verbatim string literal.
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
Precede the literal with "@" character and you can then type that.
Upvotes: 0
Reputation: 3315
try adding a "@" symbol in front of the string like this:
String.Format(@"corp\myusername")
or double up the "\" to escape the escape :)
Upvotes: 0
Reputation: 20585
using verbatim string
string username = @"corp\\myusername"
or
"\\"
= \
so "\\\\"
= \\
string username = "corp\\\\myusername"
Upvotes: 1