AWS Cognito电子邮件验证

时间:2017-09-21 04:57:42

标签: amazon-web-services xamarin amazon-cognito

我正在尝试在我的跨平台Xamarin应用中使用AWS Cognito用户池。我正确地开始在用户池中注册用户(用户显示在用户池中,并且发送带有验证码的电子邮件)。我似乎无法找出验证用户电子邮件以在用户池中确认它们的正确方法。我一直收到NotAuthorizedException。

--------编辑:以下代码块已更新为我最近的尝试--------

注册用户代码:

public async Task<Exception> RegisterUserInUserPool(String sUsername, String sPassword, String sEmail)
    {

        AmazonCognitoIdentityProviderClient oClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast2);
        CognitoUserPool oUserPool = new CognitoUserPool(sUserPoolID, sClientID, oClient);

        try
        {
            await oUserPool.SignUpAsync(sUsername, sPassword, new Dictionary<string, string> { { "email", sEmail } }, null);
            return null;
        }
        catch (Exception e)
        {
            return e;
        }
    }

我最近尝试验证用户:

public async Task<Exception> VerifyEmail(String sUsername, String sVerificationCode)
    {

        CognitoAWSCredentials oCreds = new CognitoAWSCredentials(sIdentityPoolID, Amazon.RegionEndpoint.USEast2);
        AmazonCognitoIdentityProviderClient oClient = new AmazonCognitoIdentityProviderClient(oCreds, Amazon.RegionEndpoint.USEast2);
        CognitoUserPool oUserPool = new CognitoUserPool(sUserPoolID, sClientID, oClient);
        CognitoUser oCognitoUser = new CognitoUser(sUsername, sClientID, oUserPool, oClient);

        try
        {
            await oCognitoUser.ConfirmSignUpAsync(sVerificationCode, false);
            return null;
        }
        catch (Exception e)
        {
            return e;
        }
    }

编辑:上面用于确认用户验证的更新代码返回NotAuthorizedException异常,该异常显示&#34;此标识池不支持未经身份验证的访问。&#34;
用户池允许此类确认的正确设置是什么?我的代码是否缺少任何步骤?

感谢任何帮助或澄清!

4 个答案:

答案 0 :(得分:1)

CognitoIdentityServiceProvider SDK:

使用confirmRegistration()或adminconfirmSignUp()函数。

Example Code

答案 1 :(得分:1)

我使用以下代码,效果很好

AmazonCognitoIdentityProviderClient providerClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), AWSSettings.AWS_REGION);
ConfirmSignUpRequest confirmRequest = new ConfirmSignUpRequest()
{
    Username = username,
    ClientId = AWSSettings.AWS_CLIENT_ID, //use your own client id
    ConfirmationCode = code
};
return await providerClient.ConfirmSignUpAsync(confirmRequest);

此外,AWS Cognito客户端应用程序不应具有secretId,并且不应标记ADMIN_NO_SRP_AUTH。

答案 2 :(得分:0)

我只想添加额外的信息,因为这是谷歌的第一个stackoverflow选项,适用于那些在认知电子邮件验证方面苦苦挣扎的人。

如果您要注册用户但获取电子邮件验证链接,请检查您是否设置了电子邮件转发器。

在Cognito用户池页面上转到:

应用集成&gt;域名:在此处输入域名前缀,以允许发送验证电子邮件。

这是我用来注册用户并发送确认链接的代码。

    public async Task<SignUpResponse> SignupUserAsync(CognitoUser user)
    {
        var region = "eu-west-2";
        var provider = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(),
            RegionEndpoint.GetBySystemName(region));

        var signupRequest = new SignUpRequest
        {
            ClientId = _clientId,
            Username = user.Email,
            Password = user.Password
        };
        AttributeType emailAttribute = new AttributeType
        {
            Name = "email",
            Value = user.Email
        };
        signupRequest.UserAttributes.Add(emailAttribute);
        var newUser = provider.SignUpAsync(signupRequest);
        return await newUser;
    }

CognitoUser是一个继承自IdentityUser的自定义类,可以在教程中找到,我只是复制了它。

public class CognitoUser : IdentityUser
{
    public string Password { get; set; }
    public UserStatusType Status { get; set; }
}

在下一个问题上,我相信将来不会太过分。 AHA

希望它有所帮助!

答案 3 :(得分:0)

感谢所有花时间回答的人。事情的组合促使我使用代码。我想发布适合我的代码以及一些我可以使用的提示。希望它可以帮助别人!

在用户池中注册用户:

public async Task<Exception> RegisterUserInUserPool(String sUsername, String sPassword, String sEmail)
    {

        AmazonCognitoIdentityProviderClient oClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast2);
        CognitoUserPool oUserPool = new CognitoUserPool(sUserPoolID, sClientID, oClient);

        try
        {
            await oUserPool.SignUpAsync(sUsername, sPassword, new Dictionary<string, string> { { "email", sEmail } }, null);
            return null;
        }
        catch (Exception e)
        {
            return e;
        }
    }

确认用户的电子邮件:

public async Task<Exception> VerifyEmail(String sUsername, String sVerificationCode)
    {

        AmazonCognitoIdentityProviderClient oClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast2);
        CognitoUserPool oUserPool = new CognitoUserPool(sUserPoolID, sClientID, oClient);
        CognitoUser oCognitoUser = new CognitoUser(sUsername, sClientID, oUserPool, oClient);

        try
        {
            await oCognitoUser.ConfirmSignUpAsync(sVerificationCode, false);
            return null;
        }
        catch (Exception e)
        {
            return e;
        }
    }

一些提示:

  • 了解AWS用户池和身份池之间的区别。
  • 检查垃圾邮件文件夹中的验证码。 (看起来很简单,但这让我有一段时间了)
  • .NET AWS docs对某些事情很有用。 (我认为总体上有点缺乏)
  • 对用户进行身份验证并允许他们访问AWS资源的下一步是在 CognitoUser 模型上调用StartWithSrpAuthAsync
  • 请记住,这都是使用AWSSDK.Extensions.CognitoAuthentication Nuget包。
相关问题