简体   繁体   English

System.Net.Http.dll中发生了未处理的“System.FormatException”类型异常

[英]An unhandled exception of type 'System.FormatException' occurred in System.Net.Http.dll

I am trying to send a Httprequest to web api and i am including the username and password in request header, but i get The format of value 'dGVzdGNsaWVudDAyOnBhc3MwMg==' is invalid 我正在尝试向Web api发送Httprequest,我在请求标头中包含用户名和密码,但我得到The format of value 'dGVzdGNsaWVudDAyOnBhc3MwMg==' is invalid

        HttpClientHandler handler = new HttpClientHandler();
        HttpClient client = new HttpClient(handler);
        String userName = "testclient02";
        String userPassword = "pass02";

        string authInfo = userName + ":" + userPassword;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

       // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", authInfo);//if use this i get 2 authorization tags in header- Authorization: Authorization XXXXXXXXX===
        client.DefaultRequestHeaders.Add("Authorization", authInfo.ToString());//error here

        var result = client.GetAsync(new Uri("http://localhost:007/api/XXX")).Result;

edit: This my decoding code 编辑:这是我的解码代码

  public class AuthenticationHandler : DelegatingHandler
    {
        public User ObjUser;
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                var tokens = request.Headers.GetValues("Authorization").FirstOrDefault();
                if (tokens != null)
                {
                    byte[] data = Convert.FromBase64String(tokens);
                    string decodedString = Encoding.UTF8.GetString(data);
                    string[] tokensValues = decodedString.Split(':');

                    ObjUser = new CredentialChecker().CheckCredential(tokensValues[0], tokensValues[1]);
               }
       }
}

If you are using basic authentication then create AuthenticationHeaderValue like this: 如果使用的是基本身份验证,则创建AuthenticationHeaderValue如下所示:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);

See Basic access authentication as a reference. 请参阅基本访问身份验证作为参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常” - “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了未处理的“System.FormatException”类型异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 发生类型为&#39;System.FormatException&#39;的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred System.Speech.dll 中发生类型为“System.FormatException”的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in System.Speech.dll c# 中的运行时错误 - mscorlib.dll 中发生类型为“System.FormatException”的未处理异常 - Run-time error in c# - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Microsoft.Speech.dll中发生类型&#39;System.FormatException&#39;的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred in Microsoft.Speech.dll mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常其他信息:输入字符串的格式不正确 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format 尝试解析DateTime时-mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - When trying to parse DateTime - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 在C#中将十进制转换为十六进制,并出现错误“ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常” - decimal to hexadecimal in c# with mistake “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM