Special Character in WCF

433 views Asked by At

I am using SHA-1 to encrypt password and send it to service as follows.

The following password is being encrypted.

Client Side

HttpGetCommand.cs

public class HttpGetCommand<TResult> : AbstractHttpCommand<TResult>
    where TResult : class
{
    private readonly Dictionary<string, object> _parameters = new Dictionary<string, object>();

    protected override string TypeRequest { get { return "GET"; } }

    public HttpGetCommand(string url)
        : base(url)
    { }

    public HttpGetCommand(string url, Action<TResult> successAction, Action<Exception> errorAction)
        : base(url, successAction, errorAction)
    {
    }

    public HttpGetCommand<TResult> AddParameter(object urlParameter)
    {
        AddUrlParameter(urlParameter);
        return this;
    }

    public HttpGetCommand<TResult> AddParameter(string name, object value)
    {
        _parameters.Add(name, value);
        return this;
    }

    public T AddParameter<T>(string name, object value)
        where T : HttpGetCommand<TResult>
    {
        _parameters.Add(name, value);
        return (T)this;
    }

    public override TResult Execute()
    {
        return _httpService.Get<TResult>(Url, _parameters);
    }

    public async override Task ExecuteAsync()
    {
        await _httpService.GetAsync(Url, _parameters, SuccessAction, ErrorAction);
    }

    public override string ToString()
    {
        return base.ToString() + GetParams();
    }

    private string GetParams()
    {
        if (!_parameters.Any())
        {
            return string.Empty;
        }
        var sb = new StringBuilder();
        sb.Append("?");
        foreach (var parameter in _parameters)
        {
            sb.AppendFormat("{0}&{1}", parameter.Key, parameter.Value.ToString().Replace(' ', '+'));
        }
        return sb.ToString();
    }
}

LoginCommand.cs

public class LoginCommand : HttpGetCommand<LoginResult>
{
    public LoginCommand()
        : base("Login")
    {
    }
    public LoginCommand(Action<LoginResult> successAction, Action<Exception> errorAction)
        : base("Login", successAction, errorAction)
    {
    }

    public LoginCommand SetUsername(string username)
    {
        return AddParameter<LoginCommand>("username", username);
    }

    public LoginCommand SetPassword(string password)
    {
        return AddParameter<LoginCommand>("userpassword", password);
    }
}

In the code, I am calling as follows

 return new LoginCommand()
            .SetUsername(username)
            .SetPassword(password)
            .Execute();

Service side

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Stream Login(string username, string userpassword);

But if encyrtion has special character such as "+", it does not reach out to the service.

For example if the encrypted password from the client side like

d908980/fhjdfgf89sdsd+sdsd

then it reaches to service side as follows

d908980/fhjdfgf89sdsd sdsd
1

There are 1 answers

0
Daniel A. White On

Your implementation of GetParams is flawed. You need to ensure that the query string is encoded properly and in the correct format of a query string.

sb.AppendFormat("{0}={1}&", 
    Uri.EscapeDataString(parameter.Key), 
    Uri.EscapeDataString(parameter.Value.ToString())
);