![](/img/trans.png)
[英]Unauthenticated access is not supported for this identity pool, while using DynamoDB
[英]Using DynamoDB With Cognito: Token is not from a supported provider of this identity pool
我正在以這個項目為例,為我的iOS應用程序實現注冊和登錄:
以前,我的應用程序能夠通過使用AppDelegate的didFinishLaunchingWithOptions
方法中設置的憑據提供程序來訪問DynamoDB資源。 但是,在更改項目以包括登錄和功能(如示例)之后,我看到了錯誤:
"__type":"NotAuthorizedException","message":"Token is not from a supported provider of this identity pool."
該代碼設置credentialsProvider
在AppDelegate中目前看起來是這樣的:
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId:APP_CLIENT_ID, clientSecret: APP_CLIENT_SECRET, poolId: USER_POOL_ID)
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: USER_POOL_NAME)
let pool = AWSCognitoIdentityUserPool(forKey:USER_POOL_NAME)
pool.delegate = self
self.storyboard = UIStoryboard(name: "Main", bundle: nil)
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager:pool)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
我也無法通過我的應用程序訪問任何DynamoDB數據。
根據控制台的輸出,盡管我不確定登錄過程,但注冊過程似乎可以正常工作。 我突然想到將區域從存儲DynamoDB資源的EU-West-1更改為US-East-1。 為了解決此更改,我重復了我最初允許我的應用訪問DynamoDB的相同步驟:
如果有所作為,請注意,我沒有使用我鏈接的示例項目中概述的完全相同的登錄過程。 相反,我使用了顯式登錄過程,如下所示:
let name = usernameField.text!
let user = pool!.getUser(name)
lock()
user.getSession(name, password: passwordField.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {
(task:AWSTask!) -> AnyObject! in
if task.error != nil {
self.sendErrorPopup("ERROR: Unable to sign in. Error description: " + task.error!.description)
} else {
print("Successful Login")
dispatch_async(dispatch_get_main_queue()){
self.performSegueWithIdentifier("mainViewControllerSegue", sender: self)
}
}
self.unlock()
return nil
})
方法lock()
, unlock()
和sendErrorPopup()
是與我完全相關的與UI相關的方法,因此登錄過程的開始和結束將在視覺上更加清晰。 控制台輸出始終顯示“成功登錄”,但是我想知道此代碼是否真正正確地登錄了用戶,因為錯誤消息使用戶聽起來好像未得到正確的授權。
我想到美國西部表可能沒有正確設置,但是即使試圖創建新表,我也遇到同樣的問題,所以我認為這不是問題。 就授予用戶訪問DynamoDB的權限而言,我可能還缺少一些步驟嗎? AWS Cognito的新beta用戶池系統是否改變了流程?
編輯2:
我修復了上一個問題,有一段時間,我的應用程序運行良好。 但是,當我登錄時,它突然停止加載DynamoDB數據,並顯示錯誤消息: invalid login token. Can't pass in a Cognito token.
invalid login token. Can't pass in a Cognito token.
目前,我的AppData
代碼如下所示:
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId:APP_CLIENT_ID, clientSecret: APP_CLIENT_SECRET, poolId: USER_POOL_ID)
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: USER_POOL_NAME)
let pool = AWSCognitoIdentityUserPool(forKey:USER_POOL_NAME)
pool.delegate = self
self.storyboard = UIStoryboard(name: "Main", bundle: nil)
self.credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager:pool)
let manager = IdentityProviderManager(tokens: [NSString:NSString]())
self.credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager: manager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider!)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
...而我的登錄代碼如下所示:
if locked { return }
trimRegistrationValues()
let name = usernameField.text!
let user = pool!.getUser(name)
lock()
user.getSession(name, password: passwordField.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {
(task:AWSTask!) -> AnyObject! in
if task.error != nil {
self.sendErrorPopup("ERROR: Unable to sign in. Error description: " + task.error!.description)
} else {
print("Successful Login")
let loginKey = "cognito-idp.us-east-1.amazonaws.com/" + USER_POOL_ID
var logins = [NSString : NSString]()
self.credentialsProvider!.identityProvider.logins().continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
print("ERROR: Unable to get logins. Description: " + task.error!.description)
} else {
if task.result != nil{
let prevLogins = task.result as! [NSString:NSString]
print("Previous logins: " + String(prevLogins))
logins = prevLogins
}
logins[loginKey] = name
let manager = IdentityProviderManager(tokens: logins)
self.credentialsProvider!.setIdentityProviderManagerOnce(manager)
self.credentialsProvider!.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
print("ERROR: Unable to get ID. Error description: " + task.error!.description)
} else {
print("Signed in user with the following ID:")
print(task.result)
dispatch_async(dispatch_get_main_queue()){
self.performSegueWithIdentifier("mainViewControllerSegue", sender: self)
}
}
return nil
}
}
return nil
}
}
self.unlock()
return nil
})
在我的應用程序正常工作與不正常工作之間,我沒有進行任何更改。 在測試密碼重置功能時,確實導致了“密碼重置次數過多”錯誤,但是即使在我的應用上創建了新的用戶帳戶后,該問題仍然存在,因此我認為這不是原因。 我是否正確處理登錄? 如果是這樣,我應該在哪里尋找導致此問題的其他原因?
如果您為Cognito提供了登錄名但未啟用身份池使用該登錄名提供者,通常會拋出該異常。 如果還沒有,請轉到Cognito聯合身份控制台,然后打開要嘗試使用的任何提供程序(看起來像“用戶池”),並且此錯誤應該消失。
如果您確定已進行設置,是否可以提供有關如何設置登錄名的代碼段?
在登錄名中設置ID令牌所針對的密鑰,其格式應為cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>
而不是您的USER_POOL_NAME。 該博客以及您的開發人員指南中的鏈接應說明您需要的步驟和代碼。
至於過時的登錄字典的解決方案,您需要使用此構造函數來創建憑據提供程序。 這里的identityProviderManager應該是AWSIdentityProviderManager協議的實現,並且logins方法應該將您的提供者名稱的字典映射返回給令牌。 憑證提供者每次需要身份提供者令牌時都會調用此方法。 檢查此答案以獲取更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.