public UserPageModel(int id)
        {
            User             = db.Users.Find(id);
            UserApplications = User.Applications.ToList();
            List <int> ChosenPaymentGroups = UserApplications.Select(a => a.PaymentTypeID).ToList();

            AvailableApplicationTypes = db.PaymentTypes.Where(pt => !ChosenPaymentGroups.Contains(pt.PaymentTypeID)).ToList();
        }
        public UserApplications GetUserApplicationByUserApplicationID(int _userApplicationID)
        {
            UserApplications _userApplication = null;
            string           strSql           = string.Format("SELECT * FROM UserApplications WHERE UserApplicationID = {0}", _userApplicationID);
            DataRow          dr = Goodspeed.Library.Data.SQLPlus.ExecuteDataRow(CommandType.Text, strSql);

            if (dr != null)
            {
                _userApplication = new UserApplications();
                _userApplication.UserApplicationID = _userApplicationID;
                _userApplication.ApplicationID     = Convert.ToInt32(dr["ApplicationID"]);
                _userApplication.UserID            = Convert.ToInt32(dr["UserID"]);
            }
            return(_userApplication);
        }
        //GET : /api/UserProfile
        public async Task <Object> AddApplication(AddApplicationModel input)
        {
            var    result = 0;
            var    createClientResponse = new CreateClientResponse();
            string userId = User.Claims.First(c => c.Type == "UserID").Value;

            input.UserId = userId;
            var user = await _userManager.FindByIdAsync(userId);

            HttpClientHandler clientHandler = new HttpClientHandler();

            clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); };

            // Pass the handler to httpclient(from you are calling api)
            #region Create Client Id, Secret From Identity Server
            using (var _httpClient = new HttpClient(clientHandler))
            {
                string message      = JsonConvert.SerializeObject(input);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);
                var    content      = new ByteArrayContent(messageBytes);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpRequestMessage requestMessage = new HttpRequestMessage()
                {
                    RequestUri = new Uri(_configuration["RackleUrls:IdentityServer"].ToString() + "/api/Client/Create"),
                    Method     = HttpMethod.Post,
                    Content    = content
                };

                Console.WriteLine("RCL Identity URL: " + _configuration["RackleUrls:IdentityServer"].ToString() + "/api/Client/Create");
                var response = await _httpClient.SendAsync(requestMessage);

                var data = response.Content.ReadAsStringAsync().Result;
                createClientResponse = string.IsNullOrEmpty(data) ? null : JsonConvert.DeserializeObject <CreateClientResponse>(data);
            }
            #endregion

            UserApplications UserApplicationEntity = new UserApplications();
            if (!string.IsNullOrWhiteSpace(createClientResponse?.ClientId) && !string.IsNullOrWhiteSpace(createClientResponse?.ClientSecret)) // Identity Create Request atılacak response'una göre kayıt atılacak
            {
                var UserApplicationContext         = _dataContext.Set <UserApplications>();
                var ApplicationRedirectUrisContext = _dataContext.Set <ApplicationRedirectUris>();
                var ApplicationScopesContext       = _dataContext.Set <ApplicationScopes>();

                UserApplicationEntity.Id = Guid.NewGuid();
                UserApplicationEntity.ApplicationName = input.ApplicationName;
                UserApplicationEntity.ClientId        = createClientResponse?.ClientId;
                UserApplicationEntity.Secret          = createClientResponse?.ClientSecret;
                UserApplicationEntity.UserId          = user.Id;

                UserApplicationContext.Add(UserApplicationEntity);

                if (input.RedirectUris?.Count > 0)
                {
                    var RedirectUriEntityList = new List <ApplicationRedirectUris>();
                    RedirectUriEntityList = input.RedirectUris.Select(x => new ApplicationRedirectUris
                    {
                        Id            = Guid.NewGuid(),
                        ApplicationId = UserApplicationEntity.Id,
                        RedirectUri   = x
                    }).ToList();
                    ApplicationRedirectUrisContext.AddRange(RedirectUriEntityList);
                }
                if (input.Scopes?.Count > 0)
                {
                    var ScopeEntityList = new List <ApplicationScopes>();
                    ScopeEntityList = input.Scopes.Select(x => new ApplicationScopes
                    {
                        Id            = Guid.NewGuid(),
                        ApplicationId = UserApplicationEntity.Id,
                        Scope         = x
                    }).ToList();
                    ApplicationScopesContext.AddRange(ScopeEntityList);
                }
                result = await _dataContext.SaveChangesAsync();
            }
            if (result > 0 && !string.IsNullOrWhiteSpace(createClientResponse?.ClientId) && !string.IsNullOrWhiteSpace(createClientResponse?.ClientSecret))
            {
                return
                    (new {
                    Success = true,
                    UserApplication = new {
                        ApplicationName = UserApplicationEntity.ApplicationName,
                        ClientId = UserApplicationEntity.ClientId,
                        RedirectUris = UserApplicationEntity.RedirectUris.Select(x => x.RedirectUri).ToList(),
                        Scopes = UserApplicationEntity.Scopes.Select(x => x.Scope).ToList(),
                        Secret = UserApplicationEntity.Secret
                    }
                });
            }
            return(new { Success = false });
        }
        //GET : /api/UserProfile
        public async Task <Object> SaveApplication(SaveApplicationModel input)
        {
            var    result = 0;
            var    createClientResponse = new CreateClientResponse();
            string userId = User.Claims.First(c => c.Type == "UserID").Value;

            input.UserId = userId;
            var user = await _userManager.FindByIdAsync(userId);

            #region Create Client Id, Secret From Identity Server
            using (var _httpClient = new HttpClient())
            {
                string message      = JsonConvert.SerializeObject(input);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);
                var    content      = new ByteArrayContent(messageBytes);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpRequestMessage requestMessage = new HttpRequestMessage()
                {
                    RequestUri = new Uri(_configuration["RackleUrls:IdentityServer"].ToString() + "/api/Client/Save"),
                    Method     = HttpMethod.Post,
                    Content    = content
                };

                var response = await _httpClient.SendAsync(requestMessage);

                var data = response.Content.ReadAsStringAsync().Result;
                createClientResponse = string.IsNullOrEmpty(data) ? null : JsonConvert.DeserializeObject <CreateClientResponse>(data);
            }
            #endregion

            var UserApplicationContext         = _dataContext.Set <UserApplications>();
            var ApplicationRedirectUrisContext = _dataContext.Set <ApplicationRedirectUris>();
            var ApplicationScopesContext       = _dataContext.Set <ApplicationScopes>();

            UserApplications UserApplicationEntity = new UserApplications();
            if (!string.IsNullOrWhiteSpace(createClientResponse?.ClientId)) // Identity Create Request atılacak response'una göre kayıt atılacak
            {
                var applicationEntity = await UserApplicationContext.Include(x => x.RedirectUris).SingleOrDefaultAsync(x => x.ClientId == input.ClientId);

                var redirectURiList = await ApplicationRedirectUrisContext.Where(x => x.ApplicationId == applicationEntity.Id).ToListAsync();

                foreach (var item in redirectURiList)
                {
                    if (!input.RedirectUris.Contains(item.RedirectUri))
                    {
                        ApplicationRedirectUrisContext.Remove(item);
                    }
                }
                foreach (var item in input.RedirectUris)
                {
                    if (!redirectURiList.Any(x => x.RedirectUri == item))
                    {
                        ApplicationRedirectUrisContext.Add(new ApplicationRedirectUris
                        {
                            Id            = Guid.NewGuid(),
                            RedirectUri   = item,
                            ApplicationId = applicationEntity.Id
                        });
                    }
                }
                result = await _dataContext.SaveChangesAsync();
            }
            if (result > 0 && !string.IsNullOrWhiteSpace(createClientResponse?.ClientId))
            {
                UserApplicationEntity = await UserApplicationContext.Include(x => x.RedirectUris).SingleOrDefaultAsync(x => x.ClientId == input.ClientId);

                return
                    (new
                {
                    Success = true,
                    UserApplication = new
                    {
                        ApplicationName = UserApplicationEntity.ApplicationName,
                        ClientId = UserApplicationEntity.ClientId,
                        RedirectUris = UserApplicationEntity.RedirectUris.Select(x => x.RedirectUri).ToList(),
                        Scopes = UserApplicationEntity.Scopes.Select(x => x.Scope).ToList(),
                        Secret = UserApplicationEntity.Secret
                    }
                });
            }
            return(new { Success = false });
        }