c# - Azure ACS 使用 RestSharp 获取 token 415 错误

标签 c# http azure restsharp

以下是msdn提供的用于从azure ACS(访问控制服务)获取SWT token 的代码示例:

private static string GetTokenFromACS(string scope)
{
    string wrapPassword = pwd;
    string wrapUsername = uid;

    // request a token from ACS
    WebClient client = new WebClient();
    client.BaseAddress = string.Format(
        "https://{0}.{1}", serviceNamespace, acsHostUrl);

    NameValueCollection values = new NameValueCollection();
    values.Add("wrap_name", wrapUsername);
    values.Add("wrap_password", wrapPassword);
    values.Add("wrap_scope", scope);

    byte[] responseBytes = client.UploadValues("WRAPv0.9/", "POST", values);

    string response = Encoding.UTF8.GetString(responseBytes);

    Console.WriteLine("\nreceived token from ACS: {0}\n", response);

    return HttpUtility.UrlDecode(
        response
        .Split('&')
        .Single(value => value.StartsWith("wrap_access_token=", StringComparison.OrdinalIgnoreCase))
        .Split('=')[1]);
}

我正在尝试使用 RestSharp 复制代码:

var request = new RestRequest("WRAPv0.9", Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("wrap_name", uid, ParameterType.RequestBody);
request.AddParameter("wrap_password", pwd, ParameterType.RequestBody);
request.AddParameter("wrap_scope", realm, ParameterType.RequestBody);

RestClient client = new RestClient(
    string.Format(@"https://{0}.{1}", serviceNamespace, acsHostUrl));

client.ExecuteAsync(request, Callback);

我尝试了上述代码的其他变体,但没有成功。我不断收到 415 错误,指出:

415 Unsupported Media Type T8000 Content-Type 'text/plain' is not supported. The request content type must be 'application/x-www-form-urlencoded'.

我不是 Fiddler 专家,但由于我的经验有限,我无法检查我的传出 http 请求,因为它是加密的。

非常感谢有关解决该问题的建议。

最佳答案

您可以尝试省略 AddHeader 方法调用,并将 Content-Type 设置为第一个 AddParameter
问题is described here .

关于c# - Azure ACS 使用 RestSharp 获取 token 415 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985510/

相关文章:

c# - 如何并行运行specflow特征文件?

c# - Unity C#,脚本如何知道公共(public)变量(不是属性)何时更改

Android:HTTP 通信应使用 "Accept-Encoding: gzip"

c# - 如何在linq查询中使用join?

c# - 远程属性在 MVC ASP.NET 中不起作用

android - 使用改造自定义解析器发布请求

javascript - Node.JS 函数返回 http 响应值

azure - 使用 distcp 时,本地是否支持 wasb 协议(protocol)?

当我们使用派生列表达式生成器时,Azure 数据工厂管道无法使用 cosmos DB,它的列数不超过 19

Azure 容器实例未连接 kafka 集群