首页 > 解决方案 > 在后台运行的 Fiddler 修复了访问令牌的 HttpPost。也在邮递员工作

问题描述

我无法在 .NET 控制台应用程序中获取访问令牌。我写了一个失败的单元测试。

但是,如果我在后台运行 fiddler 来解密 https 请求,则该帖子可以从邮递员那里使用,也可以在控制台应用程序中使用。

我不知道为什么提琴手让它工作,但如果它正在运行,我会得到一个访问令牌

我得到的错误是“在数据库配置文件中找不到数据库 *****”或“Client_id 或 Client_secret 无效”

代码如下:

var url = _baseUrl + "token";
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);

        var grantType = "password";
        var integrated = "N";

        var username = ConfigurationManager.AppSettings["Deltek.Username"];
        var password = ConfigurationManager.AppSettings["Deltek.Password"];
        var database = ConfigurationManager.AppSettings["Deltek.DatabaseName"];
        var clientId = ConfigurationManager.AppSettings["Deltek.ClientId"];
        var clientSecret = ConfigurationManager.AppSettings["Deltek.ClientSecret"];

        Dictionary<string, string> values = new Dictionary<string, string>();
        values.Add("username", username);
        values.Add("password", password);
        values.Add("grant_type", grantType);
        values.Add("integrated", integrated);
        values.Add("database", database);
        values.Add("client_Id", clientId);
        values.Add("client_secret", clientSecret); // _keyvaultservice.getSecret(keyvault + secretname)

        var content = new FormUrlEncodedContent(values);

        request.Content = content;
        ServicePointManager.SecurityProtocol =SecurityProtocolType.Ssl3 |SecurityProtocolType.Tls12; 
        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
       ServicePointManager
            .ServerCertificateValidationCallback += 
            (sender, cert, chain, sslPolicyErrors) => true;

        var response = await _client.SendAsync(request);
        string json = await response.Content.ReadAsStringAsync();
        return await response.Content.ReadAsAsync<DeltekToken>();

标签: .netoauthpostmantoken

解决方案


推荐阅读