远程服务器返回错误:(400)错误请求。 c#azure

时间:2016-11-12 15:34:45

标签: c# azure

当我尝试连接到我的c#app获取azure令牌时遇到问题,我测试了PostMan的url和值,所有工作正常但在应用程序中发送给我这个错误:远程服务器返回错误:(400)错误请求。

这是我用于连接的方法,主机在Azure中,但自PostMan以来一切正常,这是服务器的网址http://testinglogi.azurewebsites.net/Token

public async Task<String> Login(string username, string password) {

        HttpWebRequest request = new HttpWebRequest(new Uri(String.Format("{0}Token", BASE)));
        request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        request.Method = "POST";


        string postString = string.Format("username={0}&amp;password={1}&amp;grant_type=password",
            HttpUtility.HtmlEncode(username), HttpUtility.HtmlEncode(password));

        try
        {
            byte[] bytes = Encoding.UTF8.GetBytes(postString);
            using (Stream requestStream = await request.GetRequestStreamAsync())
        {

                requestStream.Write(bytes, 0, bytes.Length);
        }


            HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
            string json;
            using (Stream responseStream = httpResponse.GetResponseStream())
            {
                json = new StreamReader(responseStream).ReadToEnd();
            }
            TokenResponseModel tokenResponse = (TokenResponseModel)JsonConvert.DeserializeObject(json);
            return tokenResponse.AccessToken;
        }
        catch (Exception ex)
        {
            throw new SecurityException("Bad credentials", ex);
        }

这是所有日志文件

  

{System.Net.WebException:远程服务器返回错误:(400)错误请求。     在System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult)[0x0005e] /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1023     在System.Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func 2 [T,TResult] endFunction,System.Action 1[T] endAction, System.Threading.Tasks.Task 1 [TResult] promise,System.Boolean requiresSynchronization)[0x00014]在/ Users / builder / data中/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550   ---从抛出异常的先前位置开始的堆栈跟踪结束---     在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]中:143     在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00047] /runtime/compilerservices/TaskAwaiter.cs:187     在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system中的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x0002e] /runtime/compilerservices/TaskAwaiter.cs:156     在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system中的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x0000b] /runtime/compilerservices/TaskAwaiter.cs:128     在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime /中的System.Runtime.CompilerServices.TaskAwaiter`1 [TResult] .GetResult()[0x00000] compilerservices / TaskAwaiter.cs:357     at LoginRest.LoginService + d _1.1.MoveNext()[0x0017f]在C:\ Users \ isrra \ documents \ visual studio 2015 \ Projects \ LoginRest \ LoginRest \ LoginService.cs:40}

我尝试登录并使用REST获取令牌

0 个答案:

没有答案