// OAuthAuthorizationServerProvider sınıfının client erişimine izin verebilmek için ilgili ValidateClientAuthentication metotunu override ediyoruz.
        //public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        //{
        //    context.Validated();
        //    return Task.FromResult<object>(null);
        //}

        // OAuthAuthorizationServerProvider sınıfının kaynak erişimine izin verebilmek için ilgili GrantResourceOwnerCredentials metotunu override ediyoruz.
        //public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        //{
        //    // CORS ayarlarını set ediyoruz.
        //    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        //    _kullanici = await _kullanicilarService.GetKullanicilarByKullaniciAdiAsync(context.UserName);
        //    if (_kullanici == null)
        //    {
        //        context.SetError("invalid_grant", "Sistemde kayıtlı böyle bir kullanıcı bulunmamaktadır.");
        //        return;
        //    }

        //    if (_kullanici != null)
        //    {
        //        byte[] salt = Convert.FromBase64String(_kullanici.Salt);

        //        byte[] passwordSaltedHash = Utility.Hash(context.Password, salt);

        //        if (Convert.ToBase64String(passwordSaltedHash).Equals(_kullanici.SifreHash, StringComparison.InvariantCulture))
        //        {
        //            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        //            identity.AddClaim(new Claim("userName", _kullanici.KullaniciAdi));
        //            identity.AddClaim(new Claim("email", _kullanici.Email));
        //            identity.AddClaim(new Claim("userID", _kullanici.Id.ToString()));

        //            context.Validated(identity);
        //            context.Request.Context.Authentication.SignIn(identity);
        //        }
        //        else
        //            context.SetError("invalid_grant", "Kullanıcı adı veya şifre yanlış.");
        //    }
        //}

        //public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
        //{
        //    var accessToken = context.AccessToken;
        //    _kullanicilarService.InsertKullaniciOturumAsync(new KullaniciOturumModel
        //    {
        //        AuthToken = context.AccessToken,
        //        KullaniciId = _kullanici.Id,
        //        SonlanmaTarihi = DateTime.Now.AddMinutes(2)
        //    });

        //    return Task.FromResult<object>(null);
        //}
        #endregion

        #region v.2.0.0
        //public override async Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
        //{
        //    //if (context.ClientId == Clients.Client1.Id)
        //    //{
        //    //    context.Validated(Clients.Client1.RedirectUrl);
        //    //}
        //    //else if (context.ClientId == Clients.Client2.Id)
        //    //{
        //    //    context.Validated(Clients.Client2.RedirectUrl);
        //    //}
        //    //return Task.FromResult(0);
        //}

        /// <summary>
        /// ValidateClientAuthentication
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            //context.Validated();
            string clientId;
            string clientSecret;

            _logger.Debug(GetType(), null, "Checking clientId & clientSecret");
            if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
            {
                context.TryGetFormCredentials(out clientId, out clientSecret);
            }

            _logger.Debug(GetType(), null, "Is ClientId null");
            if (context.ClientId == null)
            {
                context.SetError("invalid_clientId", "ClientId parametre olarak gönderilmelidir.");
                return;
            }

            _logger.Debug(GetType(), null, "GetByClientIdAsync({0})", context.ClientId);
            _client = await _clientService.GetByClientIdAsync(context.ClientId);

            if (_client == null)
            {
                context.SetError("invalid_clientId", string.Format("Sistemde kayıtlı böyle bir client bulunmamaktadır.", context.ClientId));
                return;
            }

            //Client Secret ile işlem yapılmak istenirse açılmalıdır.
            //if (_client.ApplicationType == ApplicationTypes.WebDevelopment)
            //{
            //    if (string.IsNullOrWhiteSpace(clientSecret))
            //    {
            //        context.SetError("invalid_clientId", "Client secret should be sent.");
            //        return Task.FromResult<object>(null);
            //    }
            //    else
            //    {
            //        if (client.Secret != Helper.GetHash(clientSecret))
            //        {
            //            context.SetError("invalid_clientId", "Client secret is invalid.");
            //            return Task.FromResult<object>(null);
            //        }
            //    }
            //}

            if (!_client.Active)
            {
                context.SetError("invalid_clientId", "Client aktif değildir.");
                return;
            }

            context.OwinContext.Set <string>("as:clientAllowedOrigin", _client.AllowedOrigin);
            context.OwinContext.Set <string>("as:clientRefreshTokenLifeTime", _client.RefreshTokenLifeTime.ToString());

            context.Validated();
        }
 public IHttpActionResult GetByClientId(string clientId)
 {
     try
     {
         return(Ok(_clientService.GetByClientIdAsync(clientId).Result));
     }
     catch (Exception ex)
     {
         return(InternalServerError(ex));
     }
 }