我通过AWS配置并保护了API和其他服务。我们已经应用了RBAC样式权限系统,以允许/拒绝使用Cognito和联合身份中的组访问资源。
当用户登录系统时,他们会获得一个JWT令牌,列出他们有权访问的cognio:role,并可以通过请求该资源的临时会话令牌来执行这些操作。
我目前有一个Angular网站,它按预期工作,一旦用户登录,就可以请求会话令牌:
buildCognitoCreds(idTokenJwt: string, customRoleArn: string) {
let url = 'cognito-idp.' + CognitoUtil._REGION.toLowerCase() + '.amazonaws.com/' + CognitoUtil._USER_POOL_ID;
let logins: CognitoIdentity.LoginsMap = {};
logins[url] = idTokenJwt;
let params = {
IdentityPoolId: CognitoUtil._IDENTITY_POOL_ID, /* required */
Logins: logins,
CustomRoleArn: customRoleArn
};
let serviceConfigs: awsservice.ServiceConfigurationOptions = {};
let creds = new AWS.CognitoIdentityCredentials(params, serviceConfigs);
console.log('Creds for role: ' + customRoleArn + ':', creds);
this.setCognitoCreds(creds);
return creds;
}
我现在正尝试在.net中执行相同操作,因为我们有许多需要访问AWS资源/ api的桌面和移动(Xamarin)应用程序。
我在此处关注了AWS博客:https://aws.amazon.com/blogs/developer/cognitoauthentication-extension-library-developer-preview/ 并且我能够根据我的cognito用户池验证我的用户。
我现在陷入困境,如何获取用户有权访问的特定角色的会话令牌 - 我尝试使用以下内容:
var credentials = _authenticationService.User.GetCognitoAWSCredentials(
"us-east-1:ef964b45-939e-4ef3-91c6-xxxxxxxxxxxx", // Identity pool ID
RegionEndpoint.USEast1 // Region
);
var url = "cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx";
var logins = new Dictionary<string, string>();
logins.Add(url, _authenticationService.AuthenticationResult.IdToken);
GetCredentialsForIdentityRequest request = new GetCredentialsForIdentityRequest();
request.IdentityId = "us-east-1:ef964b45-939e-4ef3-91c6-xxxxxxxxxxxxx";
request.Logins = logins;
request.CustomRoleArn = customRoleArn;
var c = new AmazonCognitoIdentityClient(credentials, RegionEndpoint.USEast1);
var test = c.GetIdentityPoolRolesAsync("us-east-1:ef964b45-939e-4ef3-91c6-xxxxxxxxxxxxxx").Result;
var result = c.GetCredentialsForIdentityAsync(request).Result;
Console.WriteLine(result.Credentials.AccessKeyId);
但是,每次我都会收到:Amazon.CognitoIdentity.Model.NotAuthorizedException
并显示以下消息:
The ambiguous role mapping rules for: cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx denied this request.
从跟踪代码,并测试它似乎我在某种程度上需要指定CustomRoleArn,以便在从Cognito用户获取cognito时使用凭证,否则在cognito联合身份中基于令牌的规则将返回拒绝响应。 - 但我无法解决如何在AWS .net SDK ......
中做到这一点