如何根据用户授权动态jsonignore? [英] how to dynamic jsonignore according to user authorize?

查看:98
本文介绍了如何根据用户授权动态jsonignore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

有条件的属性序列化

这篇关于如何根据用户授权动态jsonignore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆