Hamed
Hamed

Reputation: 137

'+' character ignored from POST data using c# to webapi2

I have a client that Connects to Asp.net Webapi2,Using Identity & OAuth2 for Authentication.
In Authentication Process , whenever Password Field Contains '+' character.The Server Just Ignore this Character!!!(And Most Other Sign Chars Mentioned In Test below)

string data = "grant_type=password&username=" + username + "&password=" + password;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
data.PostToUrl();//This Is just pseudoCode

In Server Debug:
Sent Data : password=test+1
Received Data : password=test 1
test2
Sent Data : "+_)(&^%$#@!~"
Received Data :" _)(
"

Thanks.

Upvotes: 0

Views: 167

Answers (2)

Richard Schneider
Richard Schneider

Reputation: 35477

What is the issue? With HTTP URL a + is equivalent to a space. In fact %20 can also be used.

When sending data in a query always use UrlEncode; as in

var q = string.Format("grant_type=password&username={0}&password={1}", 
    HttpUtility.UrlEncode(username),
    HttpUtility.UrlEncode(password));

Upvotes: 2

Kayani
Kayani

Reputation: 972

HttpServerUtility.UrlEncode

this will help solve the problem with special characters such as + anad #

To use it you'll need to add a reference to System.Web (Project Explorer > References > Add reference > System.Web)

Once you've done that you can use it to encode any items you wish

Upvotes: 2

Related Questions