Skip to main content
Solved

Getting FME token using token service from c# get a bad request when pasword contains & or = characters

  • November 9, 2018
  • 1 reply
  • 16 views

xtian79
Contributor
Forum|alt.badge.img+6

Using powershell to get the current token of a user I do not have problem:

$uri = "http://myserver/fmetoken/view"
$postParams = @{user='Myuser';password='PS78U-w&'}
$foo = Invoke-WebRequest -Uri $uri -Method Post -Body $postParams

$foo.Content

i receive the token.

if I use postman to verify it, there is no issue.

but if I want to implement the same action in c# i always have a bad request:

 

var url = $"{fmeServerUrl}/fmetoken/view";
var body = $"user={settingsUserFme}&password={settingsPasswordFme}";

var bodyencoded = System.Net.WebUtility.UrlEncode(body);
var byteArray = Encoding.UTF8.GetBytes(bodyencoded);
var responseCode = 0;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = byteArray.Length;
var dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
responseresult = reader.ReadToEnd();
reader.Close();
}

 

I know this is not an FME specific problem, but if you can give me a hint, it would be appreciated

Best answer by david_r

I suspect the problem is that you're URL-encoding the entire data block, where as you should only encode the values, e.g.

var body = "user=" + Uri.EscapeDataString(settingsUserFme) + 
            "&password=" + Uri.EscapeDataString(settingsPasswordFme)
View original
Did this help you find an answer to your question?

1 reply

david_r
Celebrity
  • Best Answer
  • November 9, 2018

I suspect the problem is that you're URL-encoding the entire data block, where as you should only encode the values, e.g.

var body = "user=" + Uri.EscapeDataString(settingsUserFme) + 
            "&password=" + Uri.EscapeDataString(settingsPasswordFme)

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings