我正在尝试在我的跨平台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;
用户池允许此类确认的正确设置是什么?我的代码是否缺少任何步骤?
感谢任何帮助或澄清!
答案 0 :(得分:1)
答案 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;
}
}
一些提示:
StartWithSrpAuthAsync
。