- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(我意识到这个问题与 How to whitelist/blacklist child object fields in the ModelBinder/UpdateModel method? 非常相似,但我的情况略有不同,现在可能有更好的解决方案。)
我们公司销售基于网络的软件,最终用户可对其进行高度配置。这种灵 active 的本质意味着我们必须在运行时做一些通常在编译时完成的事情。
关于谁对大多数所有内容具有读取或读/写访问权限,有一些相当复杂的规则。
例如,以我们想要创建的模型为例:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace j6.Business.Site.Models
{
public class ModelBindModel
{
[Required]
[Whitelist(ReadAccess = true, WriteAccess = true)]
public string FirstName { get; set; }
[Whitelist(ReadAccess = true, WriteAccess = true)]
public string MiddleName { get; set; }
[Required]
[Whitelist(ReadAccess = true, WriteAccess = true)]
public string LastName { get; set; }
[Required]
[Whitelist(ReadAccess = User.CanReadSalary, WriteAccess = User.CanWriteSalary)]
public string Salary { get; set; }
[Required]
[Whitelist(ReadAccess = User.CanReadSsn, WriteAccess = User.CanWriteSsn)]
public string Ssn { get; set; }
[Required]
public string SirNotAppearingOnThisPage { get; set; }
}
}
在 Controller 中,手动“解绑”并不难。
var resetValue = null;
modelState.Remove(field);
pi = model.GetType().GetProperty(field);
if (pi == null)
{
throw new Exception("An exception occured in ModelHelper.RemoveUnwanted. Field " +
field +
" does not exist in the model " + model.GetType().FullName);
}
// Set the default value.
pi.SetValue(model, resetValue, null);
使用 HTML 帮助程序,我可以轻松访问模型元数据并抑制呈现用户无权访问的任何字段。
关键:我无法弄清楚如何在 CONTROLLER 本身的任何位置访问模型元数据以防止过度发布。
请注意,使用 [Bind(Include...)] 不是功能性解决方案,至少在没有额外支持的情况下并非如此。要包含的属性依赖于运行时(而非编译时),排除该属性不会将其从验证中删除。
ViewData.Model
为 null
ViewData.ModelMetaData
为 null
[AllowAnonymous]
[HttpPost]
// [Bind(Exclude = "Dummy1" + ",Dummy2")]
public ViewResult Index(ModelBindModel dto)
{
zzz.ModelHelper.RemoveUnwanted(ModelState, dto, new string[] {"Salary", "Ssn"});
ViewBag.Method = "Post";
if (!ModelState.IsValid)
{
return View(dto);
}
return View(dto);
}
关于如何从 Controller 访问模型元数据有什么建议吗?或者在运行时将属性列入白名单的更好方法?
更新:
我从这个相当优秀的资源中借用了一页:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=687
模型看起来像这样:
[Required]
[WhiteList(ReadAccessRule = "Nope", WriteAccessRule = "Nope")]
public string FirstName { get; set; }
[Required]
[WhiteList(ReadAccessRule = "Database.CanRead.Key", WriteAccessRule = "Database.CanWrite.Key")]
public string LastName { get; set; }
类(class):
public class WhiteList : Attribute
{
public string ReadAccessRule { get; set; }
public string WriteAccessRule { get; set; }
public Dictionary<string, object> OptionalAttributes()
{
var options = new Dictionary<string, object>();
var canRead = false;
if (ReadAccessRule != "")
{
options.Add("readaccessrule", ReadAccessRule);
}
if (WriteAccessRule != "")
{
options.Add("writeaccessrule", WriteAccessRule);
}
if (ReadAccessRule == "Database.CanRead.Key")
{
canRead = true;
}
options.Add("canread", canRead);
options.Add("always", "be there");
return options;
}
}
并将这些行添加到链接中提到的 MetadataProvider 类中:
var whiteListValues = attributes.OfType<WhiteList>().FirstOrDefault();
if (whiteListValues != null)
{
metadata.AdditionalValues.Add("WhiteList", whiteListValues.OptionalAttributes());
}
最后是系统的核心:
public static void DemandFieldAuthorization<T>(ModelStateDictionary modelState, T model)
{
var metaData = ModelMetadataProviders
.Current
.GetMetadataForType(null, model.GetType());
var props = model.GetType().GetProperties();
foreach (var p in metaData.Properties)
{
if (p.AdditionalValues.ContainsKey("WhiteList"))
{
var whiteListDictionary = (Dictionary<string, object>) p.AdditionalValues["WhiteList"];
var key = "canread";
if (whiteListDictionary.ContainsKey(key))
{
var value = (bool) whiteListDictionary[key];
if (!value)
{
RemoveUnwanted(modelState, model, p.PropertyName);
}
}
}
}
}
最佳答案
回顾一下我对你的问题的解释:
也许是这样的?
// control general access to the method with attributes
[HttpPost, SomeOtherAttributes]
public ViewResult Edit( Foo model ){
// presumably, you must know the user to apply permissions?
DemandFieldAuthorization( model, user );
// if the prior call didn't throw, continue as usual
if (!ModelState.IsValid){
return View(dto);
}
return View(dto);
}
private void DemandFieldAuthorization<T>( T model, User user ){
// read the model's property metadata
// check the user's permissions
// check the actual POST message
// throw if unauthorized
}
关于c# - 选择性地将要绑定(bind)的模型字段列入白名单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15936641/
根据这个: Selectivity is the value between 0 and 1, and it is the fraction of rows returned after applyi
我想知道C是否允许设置选择性#define。这就是我所说的选择性:如果我定义了一个结构: typedef struct mystruct{ int (*pointer_function)(struc
我正在尝试替换 pi与 math.pi使用以下 Python 函数。 def cleanup(x): return x.replace("pi", "math.pi") 我有以下字符串: a =
有没有一种方法可以只对类型声明可用的代码执行流程检查? 有一种方法可以启用每个文件的检查( header 中的 //@flow),但是一旦设置,代码的所有部分都需要类型声明(否则会记录错误,如“108
我在 openGL (openGl 1.1 win32) 中绘制了一个场景。 我使用 glClipPlane 隐藏前景对象以允许用户查看/编辑距离部分。选择是在 native 完成的,无需使用 ope
我正在开发允许第三方上传 HTML 片段的服务。在其中一些片段中,可能有指向 CSS 文件或内联 CSS 的链接。该服务有自己的 CSS 文件。 除了 iFrame 之外,还有什么方法可以让我指示特定
假设我有下面列出的用于通过 Web 服务将数据传递给客户端的类(类已简化): public class Customer { public int CustomerId { get; set;
我肯定有一个相当普遍的文档需求...... 我正在实现一个相当大的 Java 库代码库,除其他外,它包含各种类,这些类旨在在适当的抽象级别上公开给调用者/实现者。同时,代码库当然包含库的用户在使用 A
我有我的小 program.jar,它使用了巨大的 library.jar 的一小部分。 是否有一种工具可以将几个 jar 重新打包成一个,以便它可以独立运行并且尽可能小? 更新:大小很重要。 最佳答
如何为站点的某些部分强制使用 HTTPS,例如登录页面或注册页面,并为站点的其余部分使用 HTTP? 最佳答案 我最喜欢的强制转换为 https 的方法是将其作为您的 php 脚本中的第一件事。它适用
我一直在慢慢学习 Ruby(在这一点上,这可能是我投入大量时间实际学习的第一门语言)所以这对你们中的许多人来说可能是一个非常简单的问题。 我的学习玩具项目基本上是一个 roguelike。目前,我有一
我正在使用Liip Cache Control bundle处理项目中的缓存。通过使用此捆绑包,您可以像这样配置缓存: liip_cache_control: rules: -
在上个月的某个时候,一个随机网站决定在一个框架中为我公司的网站提供服务。忽略“他们在做什么?”的问题。一分钟,我使用了一些简单的 frame-buster Javascript: if (top.l
假设我有以下 Numpy 数组: array([[3, 5, 0], [7, 0, 2]]) 我现在想在值不为 0 的地方加 2。最快的方法是什么?我必须操纵相当大的多维数组? 最佳答案 在我看来:
我是 git 的新手,我正尝试在 Github 项目上进行协作。我 fork 了一个项目,添加了功能,并根据自己的需要移植到了 Android。添加的功能需要在基础项目中,而不是 android 相关
在以下两个命令中,第二个抛出异常说“未知编解码器 libfdk_aac”。谁能指出我,可能是什么问题? $> ffmpeg -loglevel verbose -re -i /var/mp4s/tes
我正在尝试 nvidia 的展开循环指令,但还没有找到有选择地打开它的方法。 假设我有这个... void testUnroll() { #pragma optionNV(unroll all
我们公司最近开始使用git-flow,我们遇到了以下问题: 我们有一个控制应用程序日志级别的 DEV_MODE bool 值,我们希望开发分支始终具有 DEV_MODE=true。 但是,在发布版本时
我试图在我的 DataFrame df 中删除 nan 值,但是我很难在不影响整行的情况下删除每一列。下面是我的 df 示例。 Advertising No Advertising nan
我已将播放服务更新到最新版本,目前为 9.2.0,我还想为谷歌播放服务使用选择性模块。 // compile 'com.google.android.gms:play-services:9.2.
我是一名优秀的程序员,十分优秀!