

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将未经身份验证的用户切换为经过身份验证的用户
<a name="switching-identities"></a>

Amazon Cognito 身份池同时支持经过身份验证的用户和未经身份验证的用户。未经身份验证的用户即使未使用您的任何身份提供商登录，也可以访问您的 Amazon 资源（IdPs）。此级别的访问可用于向尚未登录的用户显示内容。即使每个未经身份验证的用户尚未单独登录和经过身份验证，这些用户在身份池中也都具有唯一的身份。

本节介绍了用户如何选择从使用未经身份验证的身份登录切换为使用经过身份验证的身份登录。

## Android
<a name="switching-identities-1.android"></a>

用户能够以未经身份验证的来宾的身份登录您的应用程序。最终，他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符，并确保配置文件数据自动合并。

应用程序会通过 `IdentityChangedListener` 界面收到配置文件合并的消息。在界面中实施 `identityChanged` 方法以接收这些消息：

```
@override
public void identityChanged(String oldIdentityId, String newIdentityId) {
    // handle the change
}
```

## iOS – objective-C
<a name="switching-identities-1.ios-objc"></a>

用户能够以未经身份验证的来宾的身份登录您的应用程序。最终，他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符，并确保配置文件数据自动合并。

`NSNotificationCenter` 通知应用程序配置文件合并的消息：

```
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(identityIdDidChange:)
                                      name:AWSCognitoIdentityIdChangedNotification
                                      object:nil];

-(void)identityDidChange:(NSNotification*)notification {
    NSDictionary *userInfo = notification.userInfo;
    NSLog(@"identity changed from %@ to %@",
        [userInfo objectForKey:AWSCognitoNotificationPreviousId],
        [userInfo objectForKey:AWSCognitoNotificationNewId]);
}
```

## iOS – swift
<a name="switching-identities-1.ios-swift"></a>

用户能够以未经身份验证的来宾的身份登录您的应用程序。最终，他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符，并确保配置文件数据自动合并。

`NSNotificationCenter` 通知应用程序配置文件合并的消息：

```
[NSNotificationCenter.defaultCenter().addObserver(observer: self
   selector:"identityDidChange"
   name:AWSCognitoIdentityIdChangedNotification
   object:nil)

func identityDidChange(notification: NSNotification!) {
  if let userInfo = notification.userInfo as? [String: AnyObject] {
    print("identity changed from: \(userInfo[AWSCognitoNotificationPreviousId])
    to: \(userInfo[AWSCognitoNotificationNewId])")
  }
}
```

## JavaScript
<a name="switching-identities-1.javascript"></a>

### 最初未经身份验证的用户
<a name="switching-identities-1.javascript-unauth"></a>

用户最初通常具有未经身份验证的角色。对于此角色，您可以设置配置对象的凭证属性，而不设置登录属性。在这种情况下，您的默认配置可能如下所示：

```
// set the default config object
var creds = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-1:1699ebc0-7900-4099-b910-2df94f52a030'
});
AWS.config.credentials = creds;
```

### 切换为经过身份验证的用户
<a name="switching-identities-1.javascript-auth"></a>

当未经身份验证的用户登录 IdP 并且您拥有令牌时，您可以通过调用可更新凭证对象和添加登录令牌的自定义函数，来将用户从未经身份验证的用户切换为经过身份验证的用户：

```
// Called when an identity provider has a token for a logged in user
function userLoggedIn(providerName, token) {
    creds.params.Logins = creds.params.Logins || {};
    creds.params.Logins[providerName] = token;

    // Expire credentials to refresh them on the next request
    creds.expired = true;
}
```

您还可以创建 `CognitoIdentityCredentials` 对象。在这种情况下，必须重置任何现有服务对象的凭证属性，以反映更新的凭证配置信息。请参阅[使用全局配置对象](https://docs.amazonaws.cn/sdk-for-javascript/latest/developer-guide/global-config-object.html)。

有关该`CognitoIdentityCredentials`对象的更多信息，请参阅[Amazon。 CognitoIdentityCredentials](https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/CognitoIdentityCredentials.html)在 适用于 JavaScript 的 Amazon SDK API 参考中。

## Unity
<a name="switching-identities-1.unity"></a>

用户能够以未经身份验证的来宾的身份登录您的应用程序。最终，他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符，并确保配置文件数据自动合并。

您可以订阅 `IdentityChangedEvent`，以接收配置文件合并的通知：

```
credentialsProvider.IdentityChangedEvent += delegate(object sender, CognitoAWSCredentials.IdentityChangedArgs e)
{
    // handle the change
    Debug.log("Identity changed from " + e.OldIdentityId + " to " + e.NewIdentityId);
};
```

## Xamarin
<a name="switching-identities-1.xamarin"></a>

用户能够以未经身份验证的来宾的身份登录您的应用程序。最终，他们可能会决定使用其中一个支持的登录 IdPs。Amazon Cognito 将确保旧身份保留与新身份相同的唯一标识符，并确保配置文件数据自动合并。

```
credentialsProvider.IdentityChangedEvent += delegate(object sender, CognitoAWSCredentials.IdentityChangedArgs e){
    // handle the change
    Console.WriteLine("Identity changed from " + e.OldIdentityId + " to " + e.NewIdentityId);
};
```