403在C#中使用Webrequest时禁止出现错误,但在postman中有效

403在C#中使用Webrequest时禁止出现错误,但在postman中有效,c#,curl,asp.net-web-api,postman,C#,Curl,Asp.net Web Api,Postman,我有一个授权码,在调用api时,我需要将它和一些头值一起传递到主体中。当从邮递员那里尝试同样的方法时,它工作正常,但是C#Webclient抛出403错误 代码如下:- 公共字符串GetResponse(字符串AuthCode) { string url = "https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa"; Uri uri = n

我有一个授权码,在调用api时,我需要将它和一些头值一起传递到主体中。当从邮递员那里尝试同样的方法时,它工作正常,但是C#Webclient抛出403错误

代码如下:-

公共字符串GetResponse(字符串AuthCode) {

string url = "https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa";
        Uri uri = new Uri(string.Format(url));

        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = "code=" + AuthCode + "&redirect_uri=" + "http://localhost:8080";
        byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);

        // Create the request
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
        httpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " +  "MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentLength = data.Length;
        httpWebRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

        Stream stream = httpWebRequest.GetRequestStream();
        stream.Write(data, 0, data.Length);
        stream.Close();

        // Get the response
        HttpWebResponse httpResponse = null;
        try
        {
            httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception while getting resource " + ex.Message);
            return null;
        }

        string result = null;
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

        return result;
}

邮递员卷曲命令:-

从curl请求生成:

curl-X柱 '' -H'授权:基本MZE4OGQWYJQTZTRLOC00MTZLJLTG5NJATZDNLYFHMNJY2IXOKX3NIVBA0X4NWTPM01RJTJ5RWWXBW1JR0ZYMHTQMK1NHHIRCPZNIUYV5WXN0MCNVBYNMNWQHCVLPZE93DJC=' -H'缓存控制:无缓存' -H'内容类型:应用程序/x-www-form-urlencoded' -d'代码=93317468-7464-4804-b38a-43e13265c4ac&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F'


我想不出这个问题。有人能帮我解决这个问题吗?

使用RestSharp并传递正确的headers值

var client = new RestClient("https://example.com/openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa");
            var request = new RestRequest(Method.POST);

            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Authorization", "Basic MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
            request.AddParameter("undefined", "code=" + AuthCode + "&redirect_uri=http%3A%2F%2Flocalhost%3A8080", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(response.Content)))
            {
                // Deserialization from JSON  
                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Token));
                Token token = (Token)deserializer.ReadObject(ms);
            return  userinfo=  GetuserInfo(token.id_token);
            }

postData不是表单数据编码。请注意文章正文与curl的区别:
redirect\u uri=http%3A%2F%2folocalhost%3A8080%2F
尝试对redirect\u URl进行编码,但得到了相同的禁止错误:(也许您可以使用类似Fiddler的东西来显示这两种请求的完整示例?我还注意到,重定向uri在您的curl请求中以
/
结尾,但在您的代码中没有。如果重定向uri有任何问题,它将给出如下错误:-{“error\u description”:“提供的重定向URI与预先注册的值不匹配。”,“错误”:“重定向URI不匹配”}我在postman中尝试过