private String BuildSignatureFromJsonAttribute(string authSecret, SessionRequest settings)
        {
            var properties = settings.GetType().GetRuntimeProperties();
            var navBody    = new StringBuilder();
            var flag       = false;

            foreach (
                var property in properties.Where(pr => pr.GetCustomAttribute <JsonPropertyAttribute>() != null).OrderBy(pr => pr.GetCustomAttribute <JsonPropertyAttribute>().PropertyName))
            {
                var attribute = property.GetCustomAttribute <JsonPropertyAttribute>();

                if (property.PropertyType.GetTypeInfo().Namespace.Contains("Quickblox.Sdk"))
                {
                    var innerClass = property.GetValue(settings);
                    if (innerClass == null)
                    {
                        continue;
                    }

                    var innerProperties = innerClass.GetType().GetRuntimeProperties();

                    foreach (var innerProperty in innerProperties.Where(pr => pr.GetCustomAttribute <JsonPropertyAttribute>() != null).OrderBy(pr => pr.GetCustomAttribute <JsonPropertyAttribute>().PropertyName))
                    {
                        var innerAttribute = innerProperty.GetCustomAttribute <JsonPropertyAttribute>();
                        var value          = innerProperty.GetValue(innerClass);
                        if (value == null)
                        {
                            continue;
                        }

                        if (flag)
                        {
                            navBody.Append(String.Format("&{0}[{1}]={2}", attribute.PropertyName, innerAttribute.PropertyName, value));
                            continue;
                        }
                        navBody.Append(String.Format("{0}[{1}]={2}", attribute.PropertyName, innerAttribute.PropertyName, value));
                        flag = true;
                    }
                }
                else
                {
                    var value = property.GetValue(settings);

                    if (value == null)
                    {
                        continue;
                    }

                    if (flag)
                    {
                        navBody.Append(String.Format("&{0}={1}", attribute.PropertyName, value));
                        continue;
                    }
                    navBody.Append(String.Format("{0}={1}", attribute.PropertyName, value));
                    flag = true;
                }
            }

            return(cryptographicProvider.Encrypt(navBody.ToString(), authSecret));
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //setup Dependency Injection
            ServiceProvider diServiceProvider = new ServiceCollection()
                                                .AddSingleton <ILoggerFactory, LoggerFactory>()
                                                .AddLogging(bldr =>
            {
                bldr.AddConsole();
            })
                                                //add program specific implementations for interfaces
                                                .AddSingleton <ICryptographicProvider, AESProvider>()
                                                .BuildServiceProvider();

            ILogger logger = diServiceProvider.GetService <ILoggerFactory>().CreateLogger <Program>();

            try
            {
                Console.WriteLine("Please Log In");
                Console.WriteLine("-----------------");
                Console.Write("Username: "******"Password: "******"Validation against live database...\r\n");

                using (FinanceContext context = new FinanceContext())
                {
                    //var hash = HashManager.GetHash(secPassword);
                    //context.Add(new User() { UserName = userName, Password = hash });
                    //context.SaveChanges();

                    var foundUser = context.User.Where(u => u.UserName == userName).FirstOrDefault();
                    if (foundUser != null)
                    {
                        if (HashManager.Match(secPassword, foundUser.Password))
                        {
                            logger.LogInformation($"user: {userName} authenticated...\r\n");

                            ICryptographicProvider provider = diServiceProvider.GetService <ICryptographicProvider>();
                            provider.Inititalize(secPassword);

                            var encrypted = provider.Encrypt("this is a test");
                            Console.WriteLine(encrypted);
                            Console.WriteLine();
                            var decrypted = provider.Decrypt(encrypted);
                            Console.WriteLine(decrypted);
                        }
                        else
                        {
                            logger.LogInformation("incorrect password provided...\r\n");
                            Console.WriteLine("invalid user/pass.");
                        }
                    }
                    else
                    {
                        logger.LogInformation("user not found...\r\n");
                        Console.WriteLine("invalid user/pass.");
                    }
                }//end using
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error occured...\r\n");
            }
        }