C# PostAsJsonAync时不支持的\u授予\u类型

C# PostAsJsonAync时不支持的\u授予\u类型,c#,http-post,asp.net-web-api2,httpclient,C#,Http Post,Asp.net Web Api2,Httpclient,我正在尝试通过c#访问ASP.NET Web API。我编写了API,我能够使用Postman访问它并获得有效的响应。当与用户名、密码和授予类型=密码一起出现时,API返回访问令牌和刷新令牌 以下是我使用邮递员时收到的回复的屏幕截图: 但是,当我尝试使用以下C#代码时: 我得到一个错误。。。 {“错误”:“不支持的授权类型”} 我一定是做错了什么事。我错过了什么 请注意使用.Result,因为调试异步代码会让我发疯以下是使用JSON获取令牌的方法 using (var clie

我正在尝试通过c#访问ASP.NET Web API。我编写了API,我能够使用Postman访问它并获得有效的响应。当与
用户名
密码
授予类型=密码
一起出现时,API返回访问令牌和刷新令牌

以下是我使用邮递员时收到的回复的屏幕截图:

但是,当我尝试使用以下C#代码时:

我得到一个错误。。。
{“错误”:“不支持的授权类型”}

我一定是做错了什么事。我错过了什么


请注意使用
.Result
,因为调试
异步
代码会让我发疯

以下是使用JSON获取令牌的方法

        using (var client = new HttpClient())
        {
            var email = "xyz"
            var password = "abc";
            var clientId = "123"
            var clientSecret = "456";

            client.BaseAddress = new Uri(baseUrl);

            // We want the response to be JSON.
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Build up the data to POST.
            List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();

            postData.Add(new KeyValuePair<string, string>("grant_type",    "password"));
            postData.Add(new KeyValuePair<string, string>("client_id",     clientId));
            postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
            postData.Add(new KeyValuePair<string, string>("username",      email));
            postData.Add(new KeyValuePair<string, string>("password",      password));



            // Post to the Server and parse the response.
            HttpResponseMessage response = await client.PostAsJsonAsync("Token", postData);
            string jsonString            = await response.Content.ReadAsStringAsync();
            object responseData          = JsonConvert.DeserializeObject(jsonString);

            // return the Access Token.
            accessToken = ((dynamic)responseData).access_token;
        }

        return accessToken;
使用(var-client=new-HttpClient())
{
var email=“xyz”
var password=“abc”;
var clientId=“123”
var clientSecret=“456”;
client.BaseAddress=新Uri(baseUrl);
//我们希望响应是JSON。
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//建立要发布的数据。
List postData=新列表();
添加(新的KeyValuePair(“授权类型”、“密码”);
添加(新的KeyValuePair(“client_id”,clientId));
添加(新的KeyValuePair(“client_secret”,clientSecret));
添加(新的KeyValuePair(“用户名”,电子邮件));
添加(新的KeyValuePair(“密码”,password));
//Post到服务器并解析响应。
HttpResponseMessage response=wait client.postsjsonasync(“令牌”,postData);
string jsonString=await response.Content.ReadAsStringAsync();
object responseData=JsonConvert.DeserializeObject(jsonString);
//返回访问令牌。
accessToken=((动态)响应数据)。访问令牌;
}
返回accessToken;

以下是使用JSON获取令牌的方法

        using (var client = new HttpClient())
        {
            var email = "xyz"
            var password = "abc";
            var clientId = "123"
            var clientSecret = "456";

            client.BaseAddress = new Uri(baseUrl);

            // We want the response to be JSON.
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Build up the data to POST.
            List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();

            postData.Add(new KeyValuePair<string, string>("grant_type",    "password"));
            postData.Add(new KeyValuePair<string, string>("client_id",     clientId));
            postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
            postData.Add(new KeyValuePair<string, string>("username",      email));
            postData.Add(new KeyValuePair<string, string>("password",      password));



            // Post to the Server and parse the response.
            HttpResponseMessage response = await client.PostAsJsonAsync("Token", postData);
            string jsonString            = await response.Content.ReadAsStringAsync();
            object responseData          = JsonConvert.DeserializeObject(jsonString);

            // return the Access Token.
            accessToken = ((dynamic)responseData).access_token;
        }

        return accessToken;
使用(var-client=new-HttpClient())
{
var email=“xyz”
var password=“abc”;
var clientId=“123”
var clientSecret=“456”;
client.BaseAddress=新Uri(baseUrl);
//我们希望响应是JSON。
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//建立要发布的数据。
List postData=新列表();
添加(新的KeyValuePair(“授权类型”、“密码”);
添加(新的KeyValuePair(“client_id”,clientId));
添加(新的KeyValuePair(“client_secret”,clientSecret));
添加(新的KeyValuePair(“用户名”,电子邮件));
添加(新的KeyValuePair(“密码”,password));
//Post到服务器并解析响应。
HttpResponseMessage response=wait client.postsjsonasync(“令牌”,postData);
string jsonString=await response.Content.ReadAsStringAsync();
object responseData=JsonConvert.DeserializeObject(jsonString);
//返回访问令牌。
accessToken=((动态)响应数据)。访问令牌;
}
返回accessToken;

需要对内容进行URL编码。 这就是它对我的作用:

            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("X-Tenant", xTenant);
            var stringContent = String.Concat("grant_type=password&username=", HttpUtility.UrlEncode(username),
                "&password=", HttpUtility.UrlEncode(password));
            var httpContent = new StringContent(stringContent, Encoding.UTF8, "application/x-www-form-urlencoded");
            var postTask = httpClient.PostAsync(apiLoginUrl, httpContent);
            postTask.Wait();
            var postResult = postTask.Result;
            var content = postResult.Content.ReadAsStringAsync().Result;
            dynamic jsonRes = JsonConvert.DeserializeObject(content);
            string access_token = jsonRes.access_token;

内容需要进行URL编码。 这就是它对我的作用:

            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("X-Tenant", xTenant);
            var stringContent = String.Concat("grant_type=password&username=", HttpUtility.UrlEncode(username),
                "&password=", HttpUtility.UrlEncode(password));
            var httpContent = new StringContent(stringContent, Encoding.UTF8, "application/x-www-form-urlencoded");
            var postTask = httpClient.PostAsync(apiLoginUrl, httpContent);
            postTask.Wait();
            var postResult = postTask.Result;
            var content = postResult.Content.ReadAsStringAsync().Result;
            dynamic jsonRes = JsonConvert.DeserializeObject(content);
            string access_token = jsonRes.access_token;

我在这篇文章中找到了解决办法。必须使用
application/x-www-form-urlencoded
而不是JSON。您还可以将PostData作为KeyValuePair传递,而不是UserToValidate类
List PostData=new List();添加(新的KeyValuePair(“授权类型”、“密码”);添加(新的KeyValuePair(“用户名”,username));添加(新的KeyValuePair(“密码”,password))
@Paresh-你能把它作为一个答案发布出来吗?我会接受iy的。这样就有多种选择。谢谢。我在这篇文章中找到了解决办法。必须使用
application/x-www-form-urlencoded
而不是JSON。您还可以将PostData作为KeyValuePair传递,而不是UserToValidate类
List PostData=new List();添加(新的KeyValuePair(“授权类型”、“密码”);添加(新的KeyValuePair(“用户名”,username));添加(新的KeyValuePair(“密码”,password))
@Paresh-你能把它作为一个答案发布出来吗?我会接受iy的。这样就有多种选择。谢谢