-1

我正在使用 SHA-1 加密密码并将其发送到服务,如下所示。

以下密码正在被加密。

客户端

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();
    }
}

登录命令.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);
    }
}

在代码中,我调用如下

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

服务端

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

但如果加密有特殊字符,如“+”,它不会到达服务。

例如,如果来自客户端的加密密码像

d908980/fhjdfgf89sdsd+sdsd

然后它到达服务端如下

d908980/fhjdfgf89sdsd sdsd
4

1 回答 1

0

您的实施GetParams存在缺陷。您需要确保对查询字符串进行了正确编码并采用正确的查询字符串格式。

sb.AppendFormat("{0}={1}&", 
    Uri.EscapeDataString(parameter.Key), 
    Uri.EscapeDataString(parameter.Value.ToString())
);
于 2017-12-16T02:50:01.977 回答