public override void OnAuthorization(HttpActionContext actionContext)
        {
            string username  = "";
            int    userLevel = 0;

            if (actionContext.Request.Headers == null || actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            else
            {
                string id = HttpContext.Current.User.Identity.Name;

                UserModel userModel;

                if (GlobalVariable.logicType == 0)
                {
                    userModel = new EntityUsersManager().GetOneUserById(id);
                }
                else if (GlobalVariable.logicType == 1)
                {
                    userModel = new SqlUsersManager().GetOneUserById(id);
                }
                else if (GlobalVariable.logicType == 2)
                {
                    userModel = new MySqlUsersManager().GetOneUserById(id);
                }
                else
                {
                    userModel = new MongoUsersManager().GetOneUserById(id);
                }

                username  = userModel.userNickName;
                userLevel = userModel.userLevel;
                value     = (roles)userLevel;

                if (UserSecurity.Login(username, userLevel))
                {
                    if (value >= role)
                    {
                        Debug.WriteLine("OnAuthorization: " + value + ">=" + role);
                        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), null);
                    }
                    else
                    {
                        Debug.WriteLine("OnAuthorization: " + value + "<" + role);
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                    }
                }
                else
                {
                    Debug.WriteLine("OnAuthorization: " + username + " or " + userLevel + " is wrong");
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
        }
        public static bool Login(string username, int userLevel = 0)
        {
            LoginModel user;

            if (GlobalVariable.logicType == 0)
            {
                user = new EntityUsersManager().ReturnUserByNameLevel(username, userLevel);
            }
            else if (GlobalVariable.logicType == 1)
            {
                user = new SqlUsersManager().ReturnUserByNameLevel(username, userLevel);
            }
            else if (GlobalVariable.logicType == 2)
            {
                user = new MySqlUsersManager().ReturnUserByNameLevel(username, userLevel);
            }
            else
            {
                user = new MongoUsersManager().ReturnUserByNameLevel(username, userLevel);
            }

            if (user != null)
            {
                Debug.WriteLine("Login: "******"!=null");
                Debug.WriteLine("Login: userLevel is: " + user.userLevel);
                if (user.userLevel > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                Debug.WriteLine("Login: user=null");
                return(false);
            }
        }
示例#3
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null || value.ToString() == "")
            {
                return(ValidationResult.Success);
            }

            string name = value.ToString();

            IUsersRepository usersRepository = null;


            if (GlobalVariable.logicType == 0)
            {
                usersRepository = new EntityUsersManager();
            }
            else if (GlobalVariable.logicType == 1)
            {
                usersRepository = new SqlUsersManager();
            }
            else if (GlobalVariable.logicType == 2)
            {
                usersRepository = new MySqlUsersManager();
            }
            else if (GlobalVariable.logicType == 3)
            {
                usersRepository = new MongoUsersManager();
            }


            if (usersRepository.IsNameTaken(name))
            {
                Debug.WriteLine("User name " + name + " already taken!");
                return(new ValidationResult("User name " + name + " already taken!"));
            }

            Debug.WriteLine("User name " + name + " is ok!");
            return(ValidationResult.Success);
        }