C# AWS Cognito OAuth:登录请求失败

C# AWS Cognito OAuth:登录请求失败,c#,amazon-web-services,oauth-2.0,amazon-cognito,restsharp,C#,Amazon Web Services,Oauth 2.0,Amazon Cognito,Restsharp,我想跳过AWS Cognito的托管UI进行登录/授权。然而,当我试图使用登录端点获取授权代码时,我会被方法不允许的响应所击。根据AWS文档,登录端点仅接受“Get”请求。基于我对这个主题的研究,我认为可以使用带有登录凭据的“Post”方法作为登录终点(多亏了AWS文档) 有人能帮我解决这个问题吗 AWS池设置: C#代码: 我使用RestSharp作为HTTP客户端 private static void CognitoOAuthSignIn(string username,

我想跳过AWS Cognito的托管UI进行登录/授权。然而,当我试图使用登录端点获取授权代码时,我会被方法不允许的响应所击。根据AWS文档,登录端点仅接受“Get”请求。基于我对这个主题的研究,我认为可以使用带有登录凭据的“Post”方法作为登录终点(多亏了AWS文档)

有人能帮我解决这个问题吗

AWS池设置:

C#代码: 我使用RestSharp作为HTTP客户端

        private static void CognitoOAuthSignIn(string username, string password)
        {
            var CLIENT_ID = "<client_id>";
            var RESPONSE_TYPE = "code";
            var REDIRECT_URI = "https://www.google.com";
            var SCOPE = "openid";
            var AUTH_DOMAIN = "https://<domain_name>.auth.us-east-1.amazoncognito.com";
            var USERNAME = username;
            var PASSWORD = password;

            RestClient client = null;

            // 1. Get XSRF Code
            var csrfRequestUrl = $"{AUTH_DOMAIN}/oauth2/authorize?response_type={RESPONSE_TYPE}&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}";
            var csrfRequest = new RestRequest(Method.GET);
            client = new RestClient(csrfRequestUrl);
            client.CookieContainer = new CookieContainer();
            IRestResponse csrfResp = client.Execute(csrfRequest);
            var cookie = client.CookieContainer.GetCookieHeader(new Uri(AUTH_DOMAIN));
            var code = cookie.Split(';')[0].Substring(11);

            // 2. Make login request
            var loginRequestUrl = $"{AUTH_DOMAIN}/login?client_id={CLIENT_ID}&response_type={RESPONSE_TYPE}&scope={SCOPE}&redirect_uri={REDIRECT_URI}";
            client = new RestClient(loginRequestUrl);
            client.DefaultParameters[0].Value = "*/*"; // Setting "Accept" header
            client.AddDefaultHeader("Content-Type", "application/x-www-form-urlencoded");
            client.AddDefaultHeader("Accept-Encoding", "gzip,deflate");
            client.AddDefaultHeader("Accept-Language", "en-US");
            client.AddDefaultHeader("Cache-Control", "no-cache");
            client.AddDefaultHeader("Cookie", $"csrf-state=; csrf-state-legacy=; XSRF-TOKEN={code}");

            var authCodeRequest = new RestRequest(Method.POST);
            authCodeRequest.AddParameter("_csrf", code, ParameterType.GetOrPost);
            authCodeRequest.AddParameter("username", USERNAME, ParameterType.GetOrPost);
            authCodeRequest.AddParameter("password", PASSWORD, ParameterType.GetOrPost);
            authCodeRequest.RequestFormat = DataFormat.None;
            IRestResponse authCodeResp = client.Execute(authCodeRequest);
            Console.WriteLine(authCodeResp.StatusCode); //returns MethodNotAllowed
        }
private static void CognitoOAuthSignIn(字符串用户名、字符串密码)
{
var客户_ID=“”;
var响应\u TYPE=“代码”;
var REDIRECT_URI=”https://www.google.com";
var SCOPE=“openid”;
var AUTH_域=”https://.auth.us-east-1.amazoncognito.com";
var USERNAME=用户名;
var PASSWORD=密码;
RestClient=null;
//1.获取XSRF代码
var csrfRequestUrl=$“{AUTH_DOMAIN}/oauth2/authorize?response_type={response_type}&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}”;
var csrfRequest=new RestRequest(Method.GET);
client=新的RestClient(csrfRequestUrl);
client.CookieContainer=新的CookieContainer();
IRestResponse csrfResp=client.Execute(csrfRequest);
var cookie=client.CookieContainer.GetCookieHeader(新Uri(AUTH_域));
var code=cookie.Split(“;”)[0]。子字符串(11);
//2.提出登录请求
var loginRequestUrl=$“{AUTH_DOMAIN}/login?客户端id={client_id}&response_type={response_type}&scope={scope}&redirect_uri={redirect_uri}”;
客户=新的RestClient(loginRequestUrl);
client.DefaultParameters[0]。Value=“*/*”;//设置“Accept”头
client.AddDefaultHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
AddDefaultHeader(“接受编码”、“gzip、deflate”);
client.AddDefaultHeader(“接受语言”、“en-US”);
AddDefaultHeader(“缓存控制”、“无缓存”);
AddDefaultHeader(“Cookie”,$“csrf状态=;csrf状态遗留=;XSRF-TOKEN={code}”);
var authCodeRequest=new RestRequest(Method.POST);
AddParameter(“\u csrf”,code,ParameterType.GetOrPost);
AddParameter(“用户名”,用户名,参数类型.GetOrPost);
authCodeRequest.AddParameter(“密码”、密码、参数类型.GetOrPost);
authCodeRequest.RequestFormat=DataFormat.None;
IRestResponse authCodeResp=client.Execute(authCodeRequest);
Console.WriteLine(authCodeResp.StatusCode);//返回不允许的方法
}

您是否考虑过使用AWS SDK进行登录?这样,您将跳过托管UI,而无需使用登录API。我们在一个项目中有类似的要求(跳过AWS Cognito托管的UI),AWS SDK对我们来说运行良好。@HenriqueDroog使用AWS SDK登录只会将管理范围添加到访问令牌,尽管我没有将管理范围附加到我尝试登录的客户端。这对我们不起作用,因为我们需要特定于应用程序客户端的作用域来授权用户对各种API路由的访问。这个问题已经在这里讨论过了,它似乎是一个设计限制,事实上,如果您需要管理范围以外的范围,那么AWS SDK不是一个好办法。您是否考虑过使用AWS SDK进行登录?这样,您将跳过托管UI,而无需使用登录API。我们在一个项目中有类似的要求(跳过AWS Cognito托管的UI),AWS SDK对我们来说运行良好。@HenriqueDroog使用AWS SDK登录只会将管理范围添加到访问令牌,尽管我没有将管理范围附加到我尝试登录的客户端。这对我们不起作用,因为我们需要特定于应用程序客户端的作用域来授权用户对各种API路由的访问。这个问题已经在这里讨论过了,它似乎是一个设计限制,这确实是真的,如果您需要管理范围以外的范围,那么AWS SDK不是一个好办法。