c# - AWS Cognito : How to make authentication request in . 注册/登录后的网络

标签 c# asp.net-mvc rest amazon-web-services aws-cognito

使用适用于 .Net 的 AWS 开发工具包,我可以使用在 UserPool 中注册用户

AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), RegionEndpoint.USEast1);
SignUpRequest signUpRequest = new SignUpRequest()
        {
            ClientId = CLIENTAPP_ID,
            Password = user.Password,
            Username = user.Username
        };

await provider.SignUpAsync(signUpRequest);

注册后,我想验证用户身份并登录并重定向到某个授权页面。使用以下代码,我可以获得 Auth Token
CognitoUserPool userPool = new CognitoUserPool(this.POOL_ID, this.CLIENTAPP_ID, provider);
CognitoUser user = new CognitoUser(username, this.CLIENTAPP_ID, userPool, provider);

InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest()
  {
    Password = password
  };

AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
 if (authResponse.AuthenticationResult != null)
 {
  // Here i can see authResponse.AuthenticationResult.AccessToken
}

但是现在对于 GetUserAttributes 或 DeleteUser 的任何下一个请求,我如何使用返回的用户 token ?
我看到有userPool.getCurrentUser();但这在.Net SDK中不可用,我认为它在JS SDK中。

那么如何在注册/登录后发出经过身份验证的请求以使用 .Net SDK 或使用 REST Api 调用执行各种其他操作,如 UpdateAttributes 或 DeleteUser 等

请建议

最佳答案

调用 StartWithSrpAuthAsync 成功返回后,您可以使用 user反对并调用GetCognitoAWSCredentials .

CognitoAWSCredentials credentials =
    user.GetCognitoAWSCredentials("identityPoolID", RegionEndpoint.<Your_Identity_Pool_Region>);
然后使用这些凭据:
// examples
await user.UpdateAttributesAsync(...);
await user.DeleteUserAsync();

new AmazonLambdaClient(credentials, AuthenticationManager.Region);
new AmazonS3Client(credentials);
您还可以调用电话获取用户:
string accessToken = authResponse.AuthenticationResult.AccessToken;

Task<GetUserResponse> responseTask =
    provider.GetUserAsync(new GetUserRequest
    {
        AccessToken = accessToken
    });

GetUserResponse responseObject = await responseTask;
一些引用资料:
  • https://aws.amazon.com/blogs/developer/cognitoauthentication-extension-library-developer-preview/
  • https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/cognito-authentication-extension.html#more-authentication-options
  • 搜索 GetCognitoAWSCredentials页面内
  • 关于c# - AWS Cognito : How to make authentication request in . 注册/登录后的网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50057066/

    相关文章:

    c# - 将类转换为字符串以通过电子邮件发送

    c# - 自定义 MultipartFormDataStreamProvider 上传后通过 WebApi 从 SQL 下载大文件

    c# - 为什么 string Method(Object object) 可能与 Func<Object, string> 委托(delegate)类型不匹配?

    c# - 创建下载加速器

    c# - 覆盖 StructureMap 3 中的自定义注册约定

    Java Spring Boot REST API - 一对多关系的未解析的前向引用

    asp.net-mvc - 如何将通用列表传递给 Html.ActionLink(...)?

    c# - 从给定的文件路径字符串中获取最后一个文件夹的 ID 作为正在创建的新文件夹的父 ID

    java - 批量分配不安全的绑定(bind)器配置 Rest 框架,JSON http 请求 :I am not using Spring MVC

    java - html提交表单的背景是什么?