예제 #1
0
        public async Task <RequestResult> GetUserDetails(string username, string password)
        {
            RequestResult result = new RequestResult();

            try
            {
                GetUserResponse response = new GetUserResponse();

                if (cognitoUserSession != null && cognitoUserSession.IsValid())
                {
                    GetUserRequest userRequest = new GetUserRequest();
                    userRequest.AccessToken = cognitoUserSession.AccessToken;
                    response = await provider.GetUserAsync(userRequest);

                    result.Data   = response;
                    result.Status = true;
                }
                else
                {
                    //this.RefreshToken(username);
                    result.Status  = false;
                    result.Message = "Not valid session";
                }
            }
            catch (Exception ex)
            {
                result.Status  = false;
                result.Message = ex.Message;
            }

            return(result);
        }
예제 #2
0
    // we call this once after the user is authenticated, then cache it as part of the session for later retrieval
    private async Task <string> GetUserIdFromProvider(string accessToken)
    {
        // Debug.Log("Getting user's id...");
        string subId = "";

        Task <GetUserResponse> responseTask =
            _provider.GetUserAsync(new GetUserRequest
        {
            AccessToken = accessToken
        });

        GetUserResponse responseObject = await responseTask;

        // set the user id
        foreach (var attribute in responseObject.UserAttributes)
        {
            if (attribute.Name == "sub")
            {
                subId = attribute.Value;
                break;
            }
        }

        return(subId);
    }
예제 #3
0
        public override async Task Start(string _postJson, IDynamoDBContext _dynamoDBContext)
        {
            await base.Start(_postJson, _dynamoDBContext);

            User user = JsonConvert.DeserializeObject <User>(_postJson);


            var authReq = new AdminInitiateAuthRequest()
            {
                UserPoolId = ApiDefine.CognitoPoolId,
                ClientId   = ApiDefine.CognitoClientId,
                AuthFlow   = AuthFlowType.ADMIN_NO_SRP_AUTH
            };

            authReq.AuthParameters.Add("USERNAME", user.Email);
            authReq.AuthParameters.Add("EMAIL", user.Email);
            authReq.AuthParameters.Add("PASSWORD", user.Password);

            var client = new AmazonCognitoIdentityProviderClient(ApiDefine.Credentials, RegionEndpoint.USWest2);
            AdminInitiateAuthResponse authResp = await client.AdminInitiateAuthAsync(authReq);

            // AccessTokenを元にUser名を取得
            var getUserReq = new GetUserRequest()
            {
                AccessToken = authResp.AuthenticationResult.AccessToken
            };

            var getUserResp = await client.GetUserAsync(getUserReq);

            /*var req = new AdminRespondToAuthChallengeRequest()
             * {
             *  ChallengeName = ChallengeNameType.ADMIN_NO_SRP_AUTH,
             *  ClientId = ApiDefine.CognitoClientId,
             *  UserPoolId = ApiDefine.CognitoPoolId,
             *  Session = authResp.Session,
             *  ChallengeResponses = new Dictionary<string, string>() {
             *      { "USERNAME", user.Email }, { "PASSWORD", user.Password }
             *  },
             * };
             * var resp =  await client.AdminRespondToAuthChallengeAsync(req);*/
            JsPath = "cognito/my.page.js";
            string json = JsonConvert.SerializeObject(getUserResp);

            await loadJs();

            ExecJs = ExecJs.Replace("_JSON", json.Replace("\"", "\\\""));
        }
예제 #4
0
        private async Task <GetUserResponse> GetUserData(string accessToken)
        {
            var getUserRequest = new GetUserRequest();

            getUserRequest.AccessToken = accessToken;

            GetUserResponse response = null;

            try
            {
                response = await _provider.GetUserAsync(getUserRequest);
            }
            catch (Exception e)
            {
                //Function.info.Log("Cognito get user request failed. " + e.Message);
            }

            return(response);
        }
예제 #5
0
        public async Task <IActionResult> SignInUser(LoginCreds loginCreds)
        {
            IAmazonCognitoIdentityProvider provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), region);
            CognitoUserPool userPool = new CognitoUserPool(poolId, appClientId, provider);
            CognitoUser     user     = new CognitoUser(loginCreds.Email, appClientId, userPool, provider);

            InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest()
            {
                Password = loginCreds.Password
            };

            AuthFlowResponse authResponse = null;

            try
            {
                //Authenticate user and retrieve stripe Id
                authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);

                GetUserRequest getUserRequest = new GetUserRequest();
                getUserRequest.AccessToken = authResponse.AuthenticationResult.AccessToken;

                GetUserResponse getUser = await provider.GetUserAsync(getUserRequest);

                string email    = getUser.UserAttributes.Where(a => a.Name == "email").First().Value;
                string stripeId = getUser.UserAttributes.Where(a => a.Name == "custom:stripeId").First().Value;

                //Retrieve plan type from stripe
                string userType = "Not registered as a Life Time Talents user";
                StripeConfiguration.ApiKey = "sk_test_51GxEfiHhYK7K9XttqUpv12yjajZLs01TY95VhvzVfPEb5Ed8GaF3GFUV2iuhFZGkBgHoNib4iHBDlpALqWPplth6008EdMnnaw";
                var      service       = new CustomerService();
                Customer customer      = service.Get(stripeId);
                var      subscriptions = customer.Subscriptions;
                for (int i = 0; i < subscriptions.Count(); i++)
                {
                    if (subscriptions.ElementAt(i).Plan.Id.Equals(standardPlan))
                    {
                        userType = "Standard user";
                    }
                    else if (subscriptions.ElementAt(i).Plan.Id.Equals(premiumPlan))
                    {
                        userType = "Premium user";
                    }
                }
                Console.WriteLine(userType);
                if (userType == "Standard user")
                {
                    await UpdateUserLastAccessed(loginCreds.Email, "Logged In");

                    return(Redirect("/Home/Main/" + email + "/" + userType + "/" + stripeId));
                }
                else if (userType == "Premium user")
                {
                    await UpdateUserLastAccessed(loginCreds.Email, "Logged In");

                    return(Redirect("/Home/Main/" + email + "/" + userType + "/" + stripeId));
                }
                else
                {
                    return(Redirect("/?Msg=loginFailed"));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Login failed: " + ex.Message);
                return(Redirect("/?Msg=loginFailed"));
            }
        }