views:

1688

answers:

1

I have a web service method that takes a network login (string) as a parameter. The login is URL Encoded from JavaScript so the DOMAIN\USERNAME is encoded into DOMAIN%5CUSERNAME. When I use the HttpUtility.URLDecode on the string, it escapes the backslash and gives me DOMAIN\\USERNAME. I'm attempting to pass this into a profile provider (which expects just a single backslash) and am getting nothing.

I've attempted to do string.Replace() as well as RegEx.Replace() and can't seem to get rid of the 2nd backslash.

Does anyone know a way to resolve this? For now, this is just a proof of concept since I'm working on since I'm not a fan of the network username being posted as a parameter; however, I am still curious to know a way around this issue. Is there a different encoding scheme I can/should use on the JavaScript side if I can't resolve this from C#?

+6  A: 

It doesn't actually turn it into \\. That's just how it looks under the debugger. The debugger shows you the string literal you'd have to use if you wanted to put it in code. If you print the string out to the console you'll see it's fine.

using System;
using System.Web;

class Test
{
    static void Main()
    {
        string url = "DOMAIN%5CUSERNAME";
        string decoded = HttpUtility.UrlDecode(url);

        Console.WriteLine(decoded);
    }
}

Output:

DOMAIN\USERNAME
Jon Skeet
t -27 seconds :(
Joel Coehoorn