Exemple #1
0
        /// <summary>
        /// Custom GetRoleClaimHistory Method
        /// </summary>
        /// <param name="userRoleClaimId">string</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;List&lt;AspNetUserRoleClaimHistory&gt;&gt;</returns>
        /// <method>GetRoleClaimHistory(CancellationToken cancellationToken)</method>
        public async Task <List <AspNetUserRoleClaimHistory> > GetRoleClaimHistory(string userRoleClaimId, CancellationToken cancellationToken)
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(userRoleClaimId.Clean());
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/RoleClaimHistory", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                List <AspNetUserRoleClaimHistory> history = JsonConvert.DeserializeObject <List <AspNetUserRoleClaimHistory> >(jsonString);

                return(history);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #2
0
        /// <summary>
        /// Implemented IUserClaimStore GetUsersForClaimAsync Method
        /// </summary>
        /// <param name="claim">Claim</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task</returns>
        /// <method>GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken)</method>
        public async Task <IList <ApplicationUser> > GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken)
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString = JsonConvert.SerializeObject(claim, new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/FindByClaim", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                List <ApplicationUser> users = JsonConvert.DeserializeObject <List <ApplicationUser> >(jsonString);

                return(users);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #3
0
        /// <summary>
        /// Implemented IUserEmailStore FindByEmailAsync Method
        /// </summary>
        /// <param name="normalizedEmail">string</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;ApplicationUser&gt;</returns>
        /// <method>FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)</method>
        public async Task <ApplicationUser> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(normalizedEmail.Clean().ToUpper());
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/FindByNormalizedEmail", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                ApplicationUser user = JsonConvert.DeserializeObject <ApplicationUser>(jsonString);

                return(user);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #4
0
        /// <summary>
        /// Custom GetPendingRegistrations Method
        /// </summary>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;List&lt;ApplicationUser&gt;&gt;</returns>
        /// <method>CreateAsync(CancellationToken cancellationToken)</method>
        public async Task <List <ApplicationUser> > GetPendingRegistrations(CancellationToken cancellationToken)
        {
            try
            {
                string accessToken = await GetAccessToken();

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/PendingRegistrations");

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                string jsonString            = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                List <ApplicationUser> users = JsonConvert.DeserializeObject <List <ApplicationUser> >(jsonString);

                return(users);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #5
0
        /// <summary>
        /// Implemented IUserLoginStore FindByLoginAsync Method
        /// </summary>
        /// <param name="loginProvider">string</param>
        /// <param name="providerKey">string</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;ApplicationUser&gt;</returns>
        /// <method>FindByLoginAsync(string loginProvider, string providerKey, CancellationToken cancellationToken)</method>
        public async Task <ApplicationUser> FindByLoginAsync(string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            try
            {
                IdentityUserLogin <string> input = new IdentityUserLogin <string>();
                input.LoginProvider = loginProvider.Clean();
                input.ProviderKey   = providerKey.Clean();

                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(input);
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/FindByLogin", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                ApplicationUser user = JsonConvert.DeserializeObject <ApplicationUser>(jsonString);

                return(user);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #6
0
        /// <summary>
        /// Custom GetApprovedUsersAsync Method
        /// </summary>
        /// <param name="roleId">string</param>
        /// <param name="roleClaimId">int</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;List&lt;ApplicationUser&gt;&gt;</returns>
        /// <method>GetApprovedUsersAsync(string roleId, int roleClaimId, CancellationToken cancellationToken = default(CancellationToken))</method>
        public async Task <List <ApplicationUser> > GetApprovedUsersAsync(string roleId, int roleClaimId, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(new KeyValuePair <string, int>(roleId.Clean(), roleClaimId));
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "Role/GetApprovedUsers", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                List <ApplicationUser> users = JsonConvert.DeserializeObject <List <ApplicationUser> >(jsonString);

                return(users);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #7
0
        /// <summary>
        /// Implemented IRoleStore Method
        /// </summary>
        /// <param name="roleId">string</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;ApplicationRole&gt;</returns>
        /// <method>FindByIdAsync(string roleId, CancellationToken cancellationToken = default(CancellationToken))</method>
        public async Task <ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(roleId.Clean());
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "Role/FindById", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Exception(new Exception(jsonClient.GetResponseString()));
                    throw new Exception(jsonClient.GetResponseString());
                }

                jsonString = AESGCM.Decrypt(jsonClient.GetResponseObject <string>(), accessToken);
                ApplicationRole role = JsonConvert.DeserializeObject <ApplicationRole>(jsonString);

                return(role);
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                throw;
            }
        }
Exemple #8
0
        public async Task <IActionResult> ValidateCaptchaToken(string captchaToken)
        {
            if (ModelState.IsValid)
            {
                string request = "siteverify";
                request += "?secret=" + _appSettings.Authentication.reCAPTCHA.SecretKey;
                request += "&response=" + captchaToken.Trim().Clean();
                request += "&remoteip=" + _httpContextAccessor.HttpContext.GetRemoteAddress().MapToIPv4().ToString();

                JsonClient     client     = new JsonClient(" https://www.google.com/recaptcha/api/");
                HttpStatusCode statusCode = await client.SendRequest(HttpMethod.Post, request, string.Empty);

                if (client.IsResponseSuccess)
                {
                    CaptchaResponse response = client.GetResponseObject <CaptchaResponse>();
                    if (response.success)
                    {
                        if (response.action.Equals("submit", StringComparison.OrdinalIgnoreCase))
                        {
                            if (response.score > 0.6)
                            {
                                return(Ok(client.GetResponseString()));
                            }
                        }
                    }
                }

                return(BadRequest(client.GetResponseString()));
            }

            return(BadRequest("Invalid request"));
        }
Exemple #9
0
        /// <summary>
        /// Implemented IUserStore DeleteAsync Method
        /// </summary>
        /// <param name="user">ApplicationUser</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;IdentityResult&gt;</returns>
        /// <method>DeleteAsync(ApplicationUser user, CancellationToken cancellationToken)</method>
        public async Task <IdentityResult> DeleteAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            try
            {
                if (user.RevokedDate == (DateTime?)DateTime.MinValue)
                {
                    throw new Exception("Missing required revoked date");
                }

                if (string.IsNullOrEmpty(user.RevokedById))
                {
                    throw new Exception("Missing required revoked by");
                }

                string accessToken = await GetAccessToken();

                string jsonString    = JsonConvert.SerializeObject(user);
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/Update", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Warning(jsonClient.GetResponseString());
                    return(IdentityResult.Failed(
                               new IdentityError()
                    {
                        Code = jsonClient.StatusCode.ToString(),
                        Description = jsonClient.GetResponseString()
                    }
                               ));
                }
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                return(IdentityResult.Failed(
                           new IdentityError
                {
                    Code = exception.Source,
                    Description = exception.Message
                }
                           ));
            }

            return(IdentityResult.Success);
        }
        /// <summary>
        /// Get url submission quota
        /// </summary>
        /// <returns>Task&lt;UrlSubmissionQuota&gt;</returns>
        /// <method>BingWebmaster(string apiKey)</method>
        public async Task <UrlSubmissionQuota> GetUrlSubmission(string siteUrl)
        {
            string url = "GetUrlSubmissionQuota?siteUrl="
                         + siteUrl.Clean()
                         + "&apikey="
                         + _apiKey;

            JsonClient     client     = new JsonClient(_bingWebmasterUrl);
            HttpStatusCode statusCode = await client.SendRequest(HttpMethod.Get, url, string.Empty);

            UrlSubmissionQuota quota = new UrlSubmissionQuota();

            if (client.IsResponseSuccess)
            {
                Dictionary <string, object> results = client.GetResponseObject <Dictionary <string, object> >();
                if (results.Count > 0)
                {
                    quota = JsonConvert.DeserializeObject <UrlSubmissionQuota>(results.ElementAt(0).Value.ToString());
                }

                quota.StatusCode    = statusCode;
                quota.StatusMessage = statusCode.ToString();
            }
            else
            {
                quota.StatusCode    = statusCode;
                quota.StatusMessage = client.GetResponseString();
            }

            return(quota);
        }
Exemple #11
0
        /// <summary>
        /// Implemented IUserStore UpdateAsync Method
        /// </summary>
        /// <param name="user">ApplicationUser</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Task&lt;IdentityResult&gt;</returns>
        /// <method>UpdateAsync(ApplicationUser user, CancellationToken cancellationToken)</method>
        public async Task <IdentityResult> UpdateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            try
            {
                string accessToken = await GetAccessToken();

                string jsonString = JsonConvert.SerializeObject(user, new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
                string encryptString = AESGCM.Encrypt(jsonString, accessToken);

                JsonClient     jsonClient = new JsonClient(_apiBaseUrl, accessToken);
                HttpStatusCode statusCode = await jsonClient.SendRequest(HttpMethod.Post, "User/Update", encryptString);

                if (!jsonClient.IsResponseSuccess)
                {
                    _logger.Warning(jsonClient.GetResponseString());
                    return(IdentityResult.Failed(
                               new IdentityError()
                    {
                        Code = jsonClient.StatusCode.ToString(),
                        Description = jsonClient.GetResponseString()
                    }
                               ));
                }
            }
            catch (Exception exception)
            {
                _logger.Exception(exception);
                return(IdentityResult.Failed(
                           new IdentityError
                {
                    Code = exception.Source,
                    Description = exception.Message
                }
                           ));
            }

            return(IdentityResult.Success);
        }
        /// <summary>
        /// Return Bing Web Search results
        /// </summary>
        /// <param name="searchType">string</param>
        /// <param name="query">string</param>
        /// <returns>Task&lt;ResultModel&gt;</returns>
        /// <exception cref="Exception">searchType - excepted (Web, Image or Video)</exception>
        /// <method>Search(string searchType, string query)</method>
        public async Task <ResultModel> Search(string searchType, string query)
        {
            ResultModel results = new ResultModel(searchType.Trim().Clean());

            // set search query url
            string queryString = string.Empty;

            switch (results.Type)
            {
            case "Web":
                queryString += QUERY_PARAMETER;
                break;

            case "Image":
                queryString += "images/" + QUERY_PARAMETER;
                break;

            case "Video":
                queryString += "videos/" + QUERY_PARAMETER;
                break;
            }

            queryString += Uri.EscapeDataString(query.Trim().Clean());
            queryString += CUSTOM_CONFIG + _customConfigId;
            queryString += MKT_PARAMETER + "en-US";
            queryString += TEXT_DECORATIONS_PARAMETER + Boolean.TrueString;
            queryString += COUNT_PARAMETER + results.MaxResults;
            queryString += OFFSET_PARAMETER + ((results.PageNumber * results.MaxResults) - results.MaxResults);

            JsonClient client = new JsonClient(_baseUri);

            client.AddRequestHeader("Ocp-Apim-Subscription-Key", _subscriptionKey);
            results.StatusCode = await client.SendRequest(HttpMethod.Get, queryString, string.Empty);

            // wait 1 second to prevent throttling
            Thread.Sleep(1000);

            results.StatusMessage = client.GetResponseString();

            if (client.IsResponseSuccess)
            {
                Dictionary <string, object> resultDictionary = client.GetResponseObject <Dictionary <string, object> >();
                switch (results.Type)
                {
                case "Web":
                    JObject webResultObject = (JObject)resultDictionary.Where(x => x.Key == "webPages").Select(x => x.Value).FirstOrDefault();
                    results.Items         = ((JArray)webResultObject["value"]).ToObject <List <ItemModel> >();
                    results.StatusMessage = client.StatusCode.ToString();
                    break;

                case "Image":
                    Object[] imageArray = resultDictionary.Where(x => x.Key == "value").Select(x => x.Value).ToArray();
                    results.Items         = ((JArray)imageArray[0]).ToObject <List <ItemModel> >();
                    results.StatusMessage = client.StatusCode.ToString();
                    break;

                case "Video":
                    Object[] videoArray = resultDictionary.Where(x => x.Key == "value").Select(x => x.Value).ToArray();
                    results.Items         = ((JArray)videoArray[0]).ToObject <List <ItemModel> >();
                    results.StatusMessage = client.StatusCode.ToString();
                    break;
                }
            }

            return(results);
        }