增加安全验证测试
This commit is contained in:
		
							parent
							
								
									7157dac6bf
								
							
						
					
					
						commit
						ca1209c310
					
				@ -18,7 +18,21 @@ namespace Falcon.SugarApi.JWT
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 用户需要具有的角色
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public List<string> Roles { get; set; } = new List<string>();
 | 
			
		||||
        public string[] Roles { get; set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 只需要登录即可通过验证
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public ApiAuthorizationAttribute() {
 | 
			
		||||
            this.Roles = new string[] { };
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 需要登录并且具有一定的角色
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="roles">角色组</param>
 | 
			
		||||
        public ApiAuthorizationAttribute(params string[] roles) {
 | 
			
		||||
            this.Roles = roles;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <inheritdoc/>
 | 
			
		||||
        public void OnAuthorization(AuthorizationFilterContext context) {
 | 
			
		||||
@ -41,7 +55,7 @@ namespace Falcon.SugarApi.JWT
 | 
			
		||||
                Unauthorized(context);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if (this.Roles != null && this.Roles.Count > 0 && !userLogin.UserInRoles(user, this.Roles)) {
 | 
			
		||||
            if (this.Roles != null && this.Roles.Length > 0 && !userLogin.UserInRoles(user, this.Roles)) {
 | 
			
		||||
                Unauthorized(context);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -34,6 +34,6 @@ namespace Falcon.SugarApi.JWT
 | 
			
		||||
        /// <param name="userInfo">用户信息</param>
 | 
			
		||||
        /// <param name="roles">需要具有的角色组</param>
 | 
			
		||||
        /// <returns>True具有,False不具有</returns>
 | 
			
		||||
        bool UserInRoles(LoginUserInfo userInfo, List<string> roles);
 | 
			
		||||
        bool UserInRoles(LoginUserInfo userInfo, params string[] roles);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user