jagregory / cognito-local

Local emulator for Amazon Cognito
MIT License
278 stars 67 forks source link

How to change this c# code to work with cognito-local? #349

Closed ZedZipDev closed 1 year ago

ZedZipDev commented 1 year ago

I have a sample c# code which works with real AWS Cognito. How should I change the code to run it with the Cognito emulator?

        public UserRepository(IOptions<AppConfig> appConfig, UserContextManager userManager, IHttpContextAccessor httpContextAccessor)
        {
            _cloudConfig = appConfig.Value;

            var ep = RegionEndpoint.GetBySystemName(_cloudConfig.Region);
            _provider = new AmazonCognitoIdentityProviderClient(
                _cloudConfig.AccessKeyId, _cloudConfig.AccessSecretKey, ep);

            _userPool = new CognitoUserPool(_cloudConfig.UserPoolId, _cloudConfig.AppClientId, _provider);

            _userManager = userManager;
            _httpContext = httpContextAccessor.HttpContext;
        }

and the appsetting.json

  "AppConfig": {
    "Region": "us-west-2",
    "UserPoolId": "us-west-2_aBcDeFgHiJ",
    "AppClientId": "Ab12Cd34Ef56Gh78ij90",
    "AccessKeyId": "AKIA1234567890",
    "AccessSecretKey": "abcdEfghalfheqncoryedofhuehhrh"
  }

Thanx

ZedZipDev commented 1 year ago

I tried to use

 var ep = RegionEndpoint.GetBySystemName(_cloudConfig.Region);
            //ep = "http://localhost:9229/";
            _provider = new AmazonCognitoIdentityProviderClient( _cloudConfig.AccessKeyId, _cloudConfig.AccessSecretKey, "http://localhost:9229/");

but: Amazon.Runtime.AmazonClientException: 'No RegionEndpoint or ServiceURL configured'

ZedZipDev commented 1 year ago

Also:

SocketException: No such host is known.
System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

HttpRequestException: No such host is known. (cognito-idp.http:443)
jagregory commented 1 year ago

Unfortunately I don't know anything about using the AWS C# SDKs, so I can't help you here.

At a guess:

  1. The region for cognito-local should be local, not an actual AWS region
  2. I don't include the / at the end of the endpoint URL, but I don't know if that would make any difference.
krotenko-redcloud commented 10 months ago

answer is pretty simple (just in case someone else is looking): new AmazonCognitoIdentityProviderConfig { ServiceURL = "http://localhost:9229" }