Exemple #1
0
        public bool ValidateToken(HttpContext context)
        {
            bool           result;
            string         tokenValue;
            JwtTokenEntity lastToken;
            TimeSpan       timeSpan;
            int            totalSeconds;

            result = true;

            if (_applyGracePeriod == "true")
            {
                tokenValue = context.Request.Headers[_tokenName];

                if (!string.IsNullOrWhiteSpace(tokenValue))
                {
                    lastToken = _userContext.JwtTokenEntities.Where(t => t.Token == tokenValue).OrderBy(t => t.CreateDate).FirstOrDefault();

                    if (lastToken != null)
                    {
                        timeSpan     = DateTime.Now - lastToken.CreateDate;
                        totalSeconds = (int)timeSpan.TotalSeconds;

                        if (totalSeconds > _gracePeriod)
                        {
                            result = false;
                        }
                    }

                    _userContext.JwtTokenEntities.Add(new JwtTokenEntity {
                        Id = _userContext.JwtTokenEntities.Count() + 1, Token = tokenValue, CreateDate = DateTime.Now
                    });
                    _userContext.SaveChanges();
                }
            }

            return
                (result);
        }
Exemple #2
0
        private void DefaultAdd()
        {
            if (_dataBaseELContext.UserEntities.Count() == 0)
            {
                #region Role
                _dataBaseELContext.RoleEntities.Add(new RoleEntity
                {
                    Id   = 1,
                    Role = "Adminstrator"
                });

                _dataBaseELContext.RoleEntities.Add(new RoleEntity
                {
                    Id   = 2,
                    Role = "User"
                });

                _dataBaseELContext.RoleEntities.Add(new RoleEntity
                {
                    Id   = 3,
                    Role = "Supervisor"
                });
                #endregion

                #region Permission
                _dataBaseELContext.PermissionEntities.Add(new PermissionEntity
                {
                    Id         = 1,
                    Permission = "CreateUser"
                });

                _dataBaseELContext.PermissionEntities.Add(new PermissionEntity
                {
                    Id         = 2,
                    Permission = "ReadUser"
                });

                _dataBaseELContext.PermissionEntities.Add(new PermissionEntity
                {
                    Id         = 3,
                    Permission = "UpdateUser"
                });
                _dataBaseELContext.PermissionEntities.Add(new PermissionEntity
                {
                    Id         = 4,
                    Permission = "DeleteUser"
                });
                _dataBaseELContext.PermissionEntities.Add(new PermissionEntity
                {
                    Id         = 5,
                    Permission = "ApproveUser"
                });
                #endregion

                #region RolePermission
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 1,
                    PermissionId = 1,
                    // Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 1)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 1,
                    PermissionId = 2,
                    // Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 2)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 1,
                    PermissionId = 3,
                    //  Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 3)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 1,
                    PermissionId = 4,
                    // Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 4)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 1,
                    PermissionId = 5,
                    //  Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 5)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 2,
                    PermissionId = 2,
                    //Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 2)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 3,
                    PermissionId = 2,
                    //  Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 2)
                });
                _dataBaseELContext.RolePermissionEntities.Add(new RolePermissionEntity
                {
                    RoleId       = 3,
                    PermissionId = 5,
                    //  Permission = _userContext.PermissionEntities.FirstOrDefault(p => p.Id == 5)
                });
                #endregion

                #region UserRole
                _dataBaseELContext.UserRoleEntities.Add(new UserRoleEntity
                {
                    UserId = 1,
                    RoleId = 1,
                });
                _dataBaseELContext.UserRoleEntities.Add(new UserRoleEntity
                {
                    UserId = 2,
                    RoleId = 2,
                });
                _dataBaseELContext.UserRoleEntities.Add(new UserRoleEntity
                {
                    UserId = 2,
                    RoleId = 3,
                });
                _dataBaseELContext.UserRoleEntities.Add(new UserRoleEntity
                {
                    UserId = 3,
                    RoleId = 2,
                });
                #endregion

                #region User
                _dataBaseELContext.UserEntities.Add(new UserEntity
                {
                    Id        = 1,
                    UserLogin = "******",
                    Email     = "*****@*****.**",
                    Address   = "Address1",
                    FirstName = "Rabel",
                    LastName  = "Obispo",
                    NickName  = "robispo",
                    Password  = "******",
                    //Roles = new RoleEntity[] { new RoleEntity { Id = 1, Role = "Adminstrator", Permissions = new PermissionEntity[] { new PermissionEntity { Id = 1, Permission = "CreateUser" } } } }
                });

                _dataBaseELContext.UserEntities.Add(new UserEntity
                {
                    Id        = 2,
                    UserLogin = "******",
                    Email     = "*****@*****.**",
                    Address   = "Address1",
                    FirstName = "Javis",
                    LastName  = "Perez",
                    NickName  = "jperez",
                    Password  = "******",
                    //Roles = new UserRoleEntity[] { new UserRoleEntity { UserId = 2, RoleId = 3 }, new UserRoleEntity { UserId = 2, RoleId = 2 } }
                });

                _dataBaseELContext.UserEntities.Add(new UserEntity
                {
                    Id        = 3,
                    UserLogin = "******",
                    Email     = "*****@*****.**",
                    Address   = "Address3",
                    FirstName = "Jose",
                    LastName  = "De Leon",
                    NickName  = "jdeleon",
                    Password  = "******",
                    //Roles = new UserRoleEntity[] { new UserRoleEntity { UserId = 3, RoleId = 2 } }
                });
                #endregion

                #region UserOld

                /*_userContext.UserEntities.Add(new UserEntity
                 * {
                 *  Id = 1,
                 *  UserLogin = "******",
                 *  Email = "*****@*****.**",
                 *  Address = "Address",
                 *  FirstName = "Rabel",
                 *  LastName = "Obispo",
                 *  NickName = "robispo",
                 *  Password = "******",
                 *  Roles = new UserRoleEntity[] { new UserRoleEntity { UserId = 1, RoleId = 1 } }
                 * });
                 *
                 * _userContext.UserEntities.Add(new UserEntity
                 * {
                 *  Id = 2,
                 *  UserLogin = "******",
                 *  Email = "*****@*****.**",
                 *  Address = "Address1",
                 *  FirstName = "Javis",
                 *  LastName = "Perez",
                 *  NickName = "jperez",
                 *  Password = "******",
                 *  Roles = new UserRoleEntity[] { new UserRoleEntity { UserId = 2, RoleId = 3 }, new UserRoleEntity { UserId = 2, RoleId = 2 } }
                 * });
                 *
                 * _userContext.UserEntities.Add(new UserEntity
                 * {
                 *  Id = 3,
                 *  UserLogin = "******",
                 *  Email = "*****@*****.**",
                 *  Address = "Address3",
                 *  FirstName = "Jose",
                 *  LastName = "De Leon",
                 *  NickName = "jdeleon",
                 *  Password = "******",
                 *  Roles = new UserRoleEntity[] { new UserRoleEntity { UserId = 3, RoleId = 2 } }
                 * });*/
                #endregion

                _dataBaseELContext.SaveChanges();
            }
        }