Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to dynamic jsonignore according to user authorize?

I use Metadata and JsonIgnore to remove special field from being serializing.

[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; }  
    }  
}  

How can I dynamic control which field should be serialized or not. For some thing like

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; }  
    }  
} 
like image 647
T_T Avatar asked Apr 22 '13 03:04

T_T


1 Answers

Conditional property serialization

like image 66
James Newton-King Avatar answered Nov 03 '22 01:11

James Newton-King