David Whitten
David Whitten

Reputation: 573

Escape character in user name

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

Answers (4)

JustBeingHelpful
JustBeingHelpful

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

user1401452
user1401452

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

Losbear
Losbear

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

Parimal Raj
Parimal Raj

Reputation: 20585

using verbatim string

string username = @"corp\\myusername"

or

"\\" = \ so "\\\\" = \\

string username = "corp\\\\myusername"

Upvotes: 1

Related Questions