aws-samples / aws-samples/amazon-cognito-unity-integration
NotAuthorizedException : Unauthenticated access is not supported for this identity pool.
- Dominant language
- C#
- Stars
- 7
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
Got this error when ask for Identity Id (before get credentials).
You can fix the error by simply adding this line :
```
CognitoAWSCredentials credentials = new CognitoAWSCredentials(ExternalParameters.identityPool, ExternalParameters.region);
credentials.AddLogin($"cognito-idp.{ExternalParameters.region.SystemName}.amazonaws.com/{ExternalParameters.userPoolId}", _datas.IdToken);
```
Here is all the method :
```
private async Task RefreshCredentials(Action onWarningMessage)
{
CognitoAWSCredentials credentials = new CognitoAWSCredentials(ExternalParameters.identityPool, ExternalParameters.region);
credentials.AddLogin($"cognito-idp.{ExternalParameters.region.SystemName}.amazonaws.com/{ExternalParameters.userPoolId}", _datas.IdToken);
AmazonCognitoIdentityClient identityClient = new AmazonCognitoIdentityClient(credentials, ExternalParameters.region);
// Ensure you have a valid IdToken.
if (string.IsNullOrEmpty(_datas.IdToken))
{
onWarningMessage?.Invoke("IdToken is empty or invalid. Ensure the user is properly authenticated.");
return;
}
// Create the GetId request with the correct IdentityPoolId and login mapping
var idRequest = new Amazon.CognitoIdentity.Model.GetIdRequest
{
IdentityPoolId = ExternalParameters.identityPool, // Correct Identity Pool ID
Logins = new Dictionary
{
// Use the User Pool ID for login mapping, not the Identity Pool ID
{ $"cognito-idp.{ExternalParameters.region.SystemName}.amazonaws.com/{ExternalParameters.userPoolId}", _datas.IdToken }
}
};
try
{
// Get identity ID
var idResponseId = await identityClient.GetIdAsync(idRequest).ConfigureAwait(false);
if (idResponseId.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
onWarningMessage?.Invoke($"Failed to get IdentityId. Status code: [{idResponseId.HttpStatusCode}]");
return;
}
Debug.Log($"Successfully obtained IdentityId: {idResponseId.IdentityId}");
// Create request to get credentials for the identity ID
var getCredentialsRequest = new Amazon.CognitoIdentity.Model.GetCredentialsForIdentityRequest
{
IdentityId = idResponseId.IdentityId,
Logins = idRequest.Logins
};
// Get credentials for the identity ID
var idResponseCredential = await identityClient.GetCredentialsForIdentityAsync(getCredentialsRequest).ConfigureAwait(false);
if (idResponseCredential.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
onWarningMessage?.Invoke($"Failed to get credentials. Status code: [{idResponseCredential.HttpStatusCode}]");
return;
}
// Update data with obtained credentials
_datas.AccessKeyId = idResponseCredential.Credentials.AccessKeyId;
_datas.SecretKey = idResponseCredential.Credentials.SecretKey;
_datas.SessionToken = idResponseCredential.Credentials.SessionToken;
_datas.ExpireDate = idResponseCredential.Credentials.Expiration;
_datas.IdentityId = idResponseId.IdentityId;
_datas.AuthType = "Cognito";
// Print token information
Debug.Log($"CREDENTIALS | AccessKeyId [{_datas.AccessKeyId}] | ExpireDate : [{_datas.ExpireDate}]");
}
catch (AmazonCognitoIdentityException e)
{
Debug.LogError($"CREDENTIALS | MESSAGE : {e.Message} | ERROR {e.ErrorCode}");
onWarningMessage?.Invoke($"Cognito error: {e.Message}");
}
catch (Exception e)
{
Debug.LogError($"CREDENTIALS | ERROR : {e.Message}");
onWarningMessage?.Invoke($"Unexpected error: {e.Message}");
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.