public async Task <ActionResult <IEnumerable <TSCategory> > > GetAll()
        {
            string UserID = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "UserID", 10);
            await TS.AddActivityLog(UserID, "Requested Categories", MethodBase.GetCurrentMethod());


            SymmKeyAndIV ClientSymmKeyAndIV = new SymmKeyAndIV
            {
                Key = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmKey", 5),
                IV  = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmIV", 10)
            };


            if (!string.IsNullOrEmpty(ClientSymmKeyAndIV.Key))
            {
                List <TSCategory> list = await TS.GetAllCategories(GlobalFunctions.CmdGetValueFromClaim(User.Claims, "UserID", 10));

                foreach (var item in list)
                {
                    GlobalFunctions.CmdEncryptEntitySymm(item, ClientSymmKeyAndIV);
                }

                return(list);
            }
            else
            {
                return(new List <TSCategory>());
            }
        }
        public async Task <ActionResult <IEnumerable <TSFeedback> > > GetAllFeedback()
        {
            string userID = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "UserID", 10);

            await TS.AddActivityLog(userID, "Requested Feedbacks", MethodBase.GetCurrentMethod());


            SymmKeyAndIV ClientSymmKeyAndIV = new SymmKeyAndIV
            {
                Key = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmKey", 5),
                IV  = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmIV", 10)
            };


            if (!string.IsNullOrEmpty(ClientSymmKeyAndIV.Key))
            {
                List <TSFeedback> list = await TS.GetAllFeedback();

                foreach (var item in list)
                {
                    item.UserName = TS.FindUser(item.UserID, true, string.Empty).Result.FullName;
                    GlobalFunctions.CmdEncryptEntitySymm(item, ClientSymmKeyAndIV);
                }

                return(list);
            }
            else
            {
                return(new List <TSFeedback>());
            }
        }
Esempio n. 3
0
        //public Startup(IConfiguration configuration, ILogger<Startup> logger)
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            //_logger = logger;

            //_logger.LogInformation("Added TodoRepository to services");
            GlobalFunctions.CmdReadEncryptedSettings();

            TS = new TableStorage();
            bool b = TS.EnuserTables().Result;

            TSUserEntity demoUser = TS.FindUser("DemoUser", false, string.Empty).Result;

            if (demoUser is null)
            {
                TSUser newUser = new TSUser()
                {
                    Email      = "*****@*****.**",
                    Password   = "******",
                    FullName   = "Demo User",
                    UserName   = "******",
                    UserID     = TS.GetNewID("AllUsers", "LastUserID", true).Result,
                    CreateDate = DateTime.Now,
                };

                DemoUserID = newUser.UserID;
                b          = TS.AddUser(newUser).Result;
            }
            else
            {
                DemoUserID = demoUser.PartitionKey;
            }

            b = TS.AddActivityLog("AllUser", "Server started", MethodBase.GetCurrentMethod()).Result;


            _timerUserUpdater = new Timer(DoWorkUserUpdater, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(10));
            _timerReminder    = new Timer(DoWorkReminder, null, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1));
        }
        public async Task <string> Get()
        {
            await TS.AddActivityLog("AllUser", "Requested public data", MethodBase.GetCurrentMethod());

            return(GlobalFunctions.CmdGetPublicData());
        }
        private async Task GenerateToken(HttpContext context)
        {
            string UserName = GlobalFunctions.CmdAsymmetricDecrypt(context.Request.Form["UserName"]);

            UserName = UserName.Substring(0, UserName.Length - 10);

            string UserPass = GlobalFunctions.CmdAsymmetricDecrypt(context.Request.Form["UserPass"]);

            UserPass = UserPass.Substring(0, UserPass.Length - 10);

            string UserType = GlobalFunctions.CmdAsymmetricDecrypt(context.Request.Form["UserType"]);

            UserType = UserType.Substring(0, UserType.Length - 10);



            WebApiUserTypesEnum tmpWebApiUserType = (WebApiUserTypesEnum)Convert.ToInt16(UserType);


            string MachineID = GlobalFunctions.CmdAsymmetricDecrypt(context.Request.Form["MachineID"]);

            MachineID = MachineID.Substring(0, MachineID.Length - 10);

            string Par_Out_Result   = string.Empty;
            string Par_Out_UserRole = string.Empty;
            string tmp_IPAddress    = context.Connection.RemoteIpAddress.ToString();

            await TS.AddVisitor(tmp_IPAddress);

            await TS.AddActivityLog("AllUser", "Token generation for " + tmp_IPAddress, MethodBase.GetCurrentMethod());

            var identity = await GetIdentity(UserName, UserPass, tmp_IPAddress, MachineID, tmpWebApiUserType, out Par_Out_Result, out Par_Out_UserRole);

            if (identity == null)
            {
                var error_response = new
                {
                    Access_token  = "",
                    Expires_in    = 0,
                    Error_Message = Par_Out_Result
                };


                // Serialize and return the response
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(JsonConvert.SerializeObject(error_response, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));

                return;
            }

            var now = DateTime.UtcNow;

            TimeSpan span     = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
            double   unixTime = span.TotalSeconds;

            // Specifically add the jti (random nonce), iat (issued timestamp), and sub (subject/user) claims.
            // You can add other claims here, if you want:


            var claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, "tmp_User"),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(now).ToString(), ClaimValueTypes.Integer64),
                new Claim("UserID", Par_Out_Result),                                                                                         //encrypted
                new Claim("UserName", GlobalFunctions.CmdAsymmetricEncrypt(UserName.ToLower() + GlobalFunctions.GetRandomAlphaNumeric(10))), //encrypted
                //new Claim("MyClientAsymPK", context.Request.Form["MyClientAsymPK"]), //encrypted
                new Claim("ClientSymmKey", context.Request.Form["ClientSymmKey"]),                                                           //encrypted
                new Claim("ClientSymmIV", context.Request.Form["ClientSymmIV"]),                                                             //encrypted
                new Claim("MachineID", context.Request.Form["MachineID"]),                                                                   //encrypted
                new Claim("ClientIP", GlobalFunctions.CmdAsymmetricEncrypt(tmp_IPAddress + GlobalFunctions.GetRandomAlphaNumeric(10))),      //encrypted
                new Claim("roles", Par_Out_UserRole),
            };



            // Create the JWT and write it to a string
            var jwt = new JwtSecurityToken(
                issuer: _options.Issuer,
                audience: _options.Audience,
                claims: claims,
                notBefore: now,
                expires: now.Add(_options.Expiration),
                signingCredentials: _options.SigningCredentials);


            try
            {
                var response = new
                {
                    Access_token  = new JwtSecurityTokenHandler().WriteToken(jwt),
                    Expires_in    = (int)_options.Expiration.TotalSeconds,
                    Error_Message = string.Empty
                };

                // Serialize and return the response
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));
            }
            catch (Exception ex)
            {
                var response = new
                {
                    Access_token  = string.Empty,
                    Expires_in    = 0,
                    Error_Message = ex.Message
                };

                // Serialize and return the response
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));
            }
        }
        public async Task <ActionResult <IEnumerable <TSUser> > > Get()
        {
            string UserID = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "UserID", 10);
            await TS.AddActivityLog(UserID, "Requested all users", MethodBase.GetCurrentMethod());

            List <TSUser> users = new List <TSUser>();

            SymmKeyAndIV ClientSymmKeyAndIV = new SymmKeyAndIV();

            ClientSymmKeyAndIV.Key = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmKey", 5);
            ClientSymmKeyAndIV.IV  = GlobalFunctions.CmdGetValueFromClaim(User.Claims, "ClientSymmIV", 10);

            if (!ClientSymmKeyAndIV.Key.Equals("Error"))
            {
                users = await TS.GetAllUsers();

                for (int i = 0; i < users.Count; i++)
                {
                    GlobalFunctions.CmdEncryptEntitySymm(users[0], ClientSymmKeyAndIV);
                }
            }
            else
            {
                return(BadRequest("Can't get crypto key!"));
            }

            return(users);
        }