Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active April 27, 2018 10:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/719a13ba709558da1a84e38786c937b4 to your computer and use it in GitHub Desktop.
Save guitarrapc/719a13ba709558da1a84e38786c937b4 to your computer and use it in GitHub Desktop.
Sample code to Sign up Cognito UserPool with C# (not Unity or Xamarin)
using Amazon;
using Amazon.CognitoIdentityProvider.Model;
using System.Threading.Tasks;
public class SignUpCognitoUserPoolSample
{
public async Task SignupCognitoUserIntoUserPoolAsync()
{
// Identify your Cognito UserPool Provider
using(var provider = new Amazon.CognitoIdentityProvider.AmazonCognitoIdentityProviderClient(RegionEndpoint.USEast1))
{
// 2016/6/22 : As there are still bug with SecretHash, do not create UserPool with Client Secret enabled. http://stackoverflow.com/questions/37438879/resolve-unable-to-verify-secret-hash-for-client-in-amazon-cognito-userpools
// Sign up new User into your Cognito User Pool
var signup = await provider.SignUpAsync(new SignUpRequest
{
ClientId = "<YOUR USER POOL APPS CLIENT ID>",
Username = "<YOUR USERNAME TO REGISTER INTO USER POOL>",
Password = "<YOUR USERPASSWORD TO REGISTER INTO USER POOL>",,
UserAttributes = new List<Amazon.CognitoIdentityProvider.Model.AttributeType>
{
// INPUT ANY ATTRIBUTES TO REGISTER
// Must include Email and Phone, if MFA is enabled
new AttributeType
{
Name = "email",
Value = "hogemoge@contoso.com",
},
new AttributeType
{
Name = "phone_number",
Value = "<PHONE NUMBER WITH COUNTRY CODE. ex: +810311111111>",
}
}
});
signup.Dump();
}
}
}
@suneelmallela
Copy link

Getting below error, please help.
"""
"errorType" : "AggregateException",
"errorMessage" : "One or more errors occurred. (Cannot determine protocol)",

"cause" : {
"errorType" : "InvalidDataException",
"errorMessage" : "Cannot determine protocol",
"""

@almsyx
Copy link

almsyx commented Jun 2, 2017

Hi @sunilmallela,
Where you able to resolve this issue?

@PrasadGulhane
Copy link

Unable to access Amazon.CognitoIdentityProvider in c# code. Please help.

@kushwahavinay
Copy link

@revolution42
Copy link

Using anonymous credentials worked for me.

using (var cognitoClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials()))

@tabenny
Copy link

tabenny commented Jun 29, 2017

I tried the following code and I am getting an exception when I call "providerClient.SignUpAsync" with a message
"Unable to store key CognitoIdentity:IdentityId:us-east-1:d8dce20b-7e3a-454a-afe5-xxxxxxxxxxxx, got error: -34018". I am not able to find a way to specify the "userpool id" in the code and I am not able to understand what am I doing wrong here. I would really appreciate if anyone can help me here to tell me what am I doing wrong here.

CognitoAWSCredentials credentials = new CognitoAWSCredentials(
"us-east-1:d8dce20b-7e3a-454a-afe5-xxxxxxxxxxxx",
RegionEndpoint.USEast1
);
AmazonCognitoIdentityProviderClient providerClient = new AmazonCognitoIdentityProviderClient(credentials,RegionEndpoint.USEast1);

        SignUpRequest sr = new SignUpRequest();

		sr.ClientId = "71tu9de9v6murgolfxxxxxxxxx";  
		sr.Username = email;
		sr.Password = password;
        sr.SecretHash = "1b7i7kdjcdij03309j0354d8tubo62it33gj4ulkxxxxxxxxxxxx";
		sr.UserAttributes = new List<AttributeType> {
			new AttributeType
			{
				Name = "email",
				Value = email,
			},
			 new AttributeType
				{
					Name = "phone_number",
					Value = phone,
				}
		};
        try{

            SignUpResponse result = await providerClient.SignUpAsync(sr);
        }

@tabenny
Copy link

tabenny commented Jul 1, 2017

The error "Unable to store key CognitoIdentity:IdentityId:us-east-1:d8dce20b-7e3a-454a-afe5-xxxxxxxxxxxx, got error: -34018" is gone after adding Entitlements.pist to iOS Bundle Signing option and adding provisioning profile, but now I am getting the error "Cannot Determine Pool". I tried AnonymousAWSCredentials and now the error is different that secret hash could not be verified..

Thank you,

Benny

@JYL123
Copy link

JYL123 commented Jul 4, 2017

Hello, anyone knows what is "signup.Dump()" at the end? I have an error which says "signUpResponse does not contain a definition of Dump".

@VagyokC4
Copy link

@JYL123 You can remove that line... It sounds like the code was run in LinqPad which has a .Dump() function to dump into the console.

@dailenspencer
Copy link

Does anyone know how to port this into C# for Unity? I'm not sure if they've released CognitoIdentityProvider for Unity

Copy link

ghost commented Mar 6, 2018

Hey, I've ported the Cognito Identity Provider from the AWS SDK for .NET 4.5, so that you can use it in Unity, using the .NET Framework 2.0:

https://github.com/SolidWhiteSven/amazon-cognito-unity

All the Best,

Sven

@anildbest83
Copy link

After SignUp, how can i UpdateAttributes of Logged-In user? i can not find userPool.getCurrentUser(); in .Net SDK, then how can i get current user and update his attributes in C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment