C# Azure ACS使用REST获取令牌415错误

C# Azure ACS使用REST获取令牌415错误,c#,http,azure,restsharp,C#,Http,Azure,Restsharp,以下是msdn提供的用于从azure ACS(访问控制服务)获取SWT令牌的代码示例: 我正在尝试使用RestSharp复制代码: 我尝试了上述代码的其他变体,但没有成功。我一直收到一个415错误,说明: 415不支持的媒体类型T8000内容类型“文本/普通”不是 支持。请求内容类型必须为 “application/x-www-form-urlencoded” 我不是一个提琴专家,但由于我的经验有限,我无法检查我发出的http请求,因为它是加密的 我希望您能提供解决此问题的建议。您可以尝试省

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

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

我尝试了上述代码的其他变体,但没有成功。我一直收到一个415错误,说明:

415不支持的媒体类型T8000内容类型“文本/普通”不是 支持。请求内容类型必须为 “application/x-www-form-urlencoded”

我不是一个提琴专家,但由于我的经验有限,我无法检查我发出的http请求,因为它是加密的


我希望您能提供解决此问题的建议。

您可以尝试省去
AddHeader
方法调用,而是将内容类型设置为第一个
AddParameter

这个问题

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