Thursday, April 9, 2009

Wrap Username and Password as Authentication token in .Net Web Service

The following code block was written in C# to combine username and password together. As you can notice, strings is a sequences of characters ending in '\0' , which here I used Convert.ToChar(0) to generate.

 

        public static String WrapUsernamePassword(String username, String password)
{
String package = username;
package += Convert.ToChar(0);
package += password;
return package;
}

public static void ParseToken(String token, out String username, out String password)
{
String[] tokenParts = token.Split(new Char[] {Convert.ToChar(0)});
if(tokenParts.Length == 2)
{
username = tokenParts[0];
password = tokenParts[1];
}
else
{
username = "";
password = "";
}
}