【发布时间】:2019-08-19 10:30:45
【问题描述】:
我正在尝试将 AWS Cognito 集成到网站中。我试图强制用户更改自己的密码。我已经有一种自愿密码重置的方法,我试图用它来强制密码重置。方法如下:
internal async Task<bool> ResetPassword(string username, string oldPassword, string newPassword) {
AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());
CognitoUserPool userPool = new CognitoUserPool(CognitoHelper.POOL_ID, CognitoHelper.CLIENTAPP_ID, provider);
CognitoUser user = new CognitoUser(username, CognitoHelper.CLIENTAPP_ID, userPool, provider);
InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest() {
Password = oldPassword
};
AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
await user.ChangePasswordAsync(oldPassword, newPassword);
return true;
} // ResetPassword
当我在自愿密码重置时调用此方法时,它工作正常。在强制密码重置时,“StartWithSrpAuthAsync”会引发异常,抱怨“用户需要密码重置”。不开玩笑 - 这就是我尝试更改密码的原因。
问题是“ChangePasswordAsync”方法要求用户在被调用之前进行身份验证。我无法对用户进行身份验证,因为需要重置密码,但我无法更改密码,因为需要先对用户进行身份验证。
我尝试通过捕获“用户需要重置密码”异常来解决我的问题,希望用户无论如何都已通过身份验证。不幸的是没有运气:
internal async Task<bool> ResetPassword(string username, string oldPassword, string newPassword) {
AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());
CognitoUserPool userPool = new CognitoUserPool(CognitoHelper.POOL_ID, CognitoHelper.CLIENTAPP_ID, provider);
CognitoUser user = new CognitoUser(username, CognitoHelper.CLIENTAPP_ID, userPool, provider);
InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest() {
Password = oldPassword
};
try {
AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
await user.ChangePasswordAsync(oldPassword, newPassword);
} catch (Exception exp) {
if (exp.Message == "Password reset required for the user") {
await user.ChangePasswordAsync(oldPassword, newPassword);
} else {
throw exp;
} // if else
} // try catch
return true;
} // ResetPassword
有什么想法吗?
【问题讨论】:
标签: c# .net amazon-web-services amazon-cognito