如何根据用户授权动态jsonignore?

T_T*_*T_T 7 json.net asp.net-web-api

我使用Metadata和JsonIgnore来删除序列化中的特殊字段.

[Authorize(Roles = "admin")]
public class UserController : ApiController
{
    public IEnumerable<user> Get()
    {
        using (var mydb = new ModelContainer())
        {
            return mydb.userSet.ToList();
        }
    }
}

[MetadataType(typeof(user_Metadata))]  
public partial class user
{  
    private class user_Metadata  
    {  
        [JsonIgnore]  
        public virtual password { get; set; }  

        public virtual adminFile { get; set; }  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

如何动态控制应该序列化哪个字段.对于某些事情

public partial class user
{  
    private class user_Metadata  
    {  
        [JsonIgnore]  
        public virtual password { get; set; }  
        [Roes == admin?JsonIgnore:JsonNotIgnore] //some thing like this
        public virtual adminFile { get; set; }  
    }  
} 
Run Code Online (Sandbox Code Playgroud)