Last active
June 4, 2022 03:36
-
-
Save exceedsystem/aaf6f8cd4a7aa22796489259b42830e0 to your computer and use it in GitHub Desktop.
AWS Cognito SignUp/Confirm/SignIn example .NET 6(C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Amazon.Extensions.CognitoAuthentication" Version="2.2.2" /> | |
<PackageReference Include="AWSSDK.CognitoIdentity" Version="3.7.0.159" /> | |
<PackageReference Include="AWSSDK.CognitoIdentityProvider" Version="3.7.2.23" /> | |
<PackageReference Include="AWSSDK.Core" Version="3.7.11.3" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.exceedsystem.net/2022/05/21/how-to-implement-user-authentication-using-aws-cognito-in-csharp/ | |
// License: MIT | |
using Amazon; | |
using Amazon.Runtime; | |
using Amazon.CognitoIdentityProvider; | |
using Amazon.CognitoIdentityProvider.Model; | |
using Amazon.Extensions.CognitoAuthentication; | |
// Pool ID of the user pool | |
const string USER_POOL_ID = "region_xxxx"; | |
// App client ID of the app client | |
const string APP_CLIENT_ID = "xxxx"; | |
// Account user name | |
const string USER_NAME = "Foo"; | |
// Account password | |
const string PASSWORD = "P@ssw0rd"; | |
// Account e-mail address | |
const string EMAIL = "mail@example.com"; | |
// Confirmation code | |
const string CONFIRMATION_CODE = "123456"; | |
var cognitoIdClient = new AmazonCognitoIdentityProviderClient( | |
new AnonymousAWSCredentials(), | |
RegionEndpoint.APNortheast1); | |
// 1. Sign up for a new account | |
await SignUp(cognitoIdClient, APP_CLIENT_ID, USER_NAME, PASSWORD, EMAIL); | |
// 2. Confirm e-mail address with a confirmation code | |
await Confirm(cognitoIdClient, USER_NAME, APP_CLIENT_ID, CONFIRMATION_CODE); | |
// 3. Sign in with the new account | |
await SignIn(cognitoIdClient, USER_POOL_ID, APP_CLIENT_ID, USER_NAME, PASSWORD); | |
static async Task SignUp(AmazonCognitoIdentityProviderClient cognitoIdClient, string clientId, string userName, string password, string email) | |
{ | |
try | |
{ | |
var regRequest = new SignUpRequest | |
{ | |
ClientId = clientId, | |
Username = userName, | |
Password = password, | |
UserAttributes = { new AttributeType { Name = "email", Value = email } } | |
}; | |
var ret = await cognitoIdClient.SignUpAsync(regRequest); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"{e.Message}"); | |
} | |
} | |
static async Task Confirm(AmazonCognitoIdentityProviderClient cognitoIdClient, string userName, string clientId, string confirmationCode) | |
{ | |
try | |
{ | |
var confirmSignUpRequest = new ConfirmSignUpRequest | |
{ | |
Username = userName, | |
ClientId = clientId, | |
ConfirmationCode = confirmationCode, | |
}; | |
var ret = await cognitoIdClient.ConfirmSignUpAsync(confirmSignUpRequest); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"{e.Message}"); | |
} | |
} | |
static async Task SignIn(AmazonCognitoIdentityProviderClient cognitoIdClient, string userPoolId, string clientId, string userName, string password) | |
{ | |
try | |
{ | |
var cognitoUserPool = new CognitoUserPool(userPoolId, clientId, cognitoIdClient); | |
var cognitoUser = new CognitoUser(userName, clientId, cognitoUserPool, cognitoIdClient); | |
var ret = await cognitoUser.StartWithSrpAuthAsync(new InitiateSrpAuthRequest { Password = password }); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"{e.Message}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment