所以我有一个引用类库的Web应用程序项目。 像这样:
MyWebApp
DatatableLibrary
----- MyDataTableClass.cs
在我看来,我正在进行一个暴露MyDataTableClass AssemblyQualifiedName的ajax调用。对于前:
DatatableLibrary.MyDataTableClass, DatatableLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
为什么呢?所以我可以在控制器中做到这一点:
public JsonResult DatatableRequest(string className){ Type.GetType(className); //more stuff + response}
我觉得在View中暴露类和汇编名称中有价值的信息的风险很大,对吗?这里有诀窍或解决方法吗?
答案 0 :(得分:0)
关于安全性,我只能考虑两个选项:
DatatableRequest
并检查该用户/调用者是否具有访问该程序集的权限。DULL
个对象。 DULL
我指的是一个空的或没有做任何事情的对象,或者如果你想要,你甚至可以返回NULL
但是,你为什么需要这样做?
我会选择第一个选项并实现过滤器,以便在需要时可以在不同的地方重复使用。
像这样:过滤强>
public class ValidateAssemblyAttribute : ActionFilterAttribute
{
public string classNameField = "className";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Get the field through the helper
string className = filterContext.GetValue<string>(classNameField);
// User 2 doesn't have access to xpto.dll
if (Membership.CurrentUserId == 2 && className == "xpto.dll")
AuthorizationHelpers.NoPermissions(filterContext);
base.OnActionExecuting(filterContext); // Execute base
}
}
<强>助手强>
public static class AuthorizationHelpers
{
public static T GetValue<T>(this ActionExecutingContext filterContext, string Field)
{
if (Field != null)
{
// Get the value provider for the given field name
var value = filterContext.Controller.ValueProvider.GetValue(Field);
// If the value provider has an attemped value (value), proceed
if (value != null && !string.IsNullOrEmpty(value.AttemptedValue))
{
// Get the underlying type of the generic parameter and return the true type: [Nullable<
Type underType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
// Get the TypeConvert to change string (AttemptedValue) to `T`.
TypeConverter conv = TypeDescriptor.GetConverter(underType);
// Create an auxiliar variable that will contain the result.
T result = default(T);
// See if the converted can convert the AttemptedValue to `T`
if (conv.IsValid(value.AttemptedValue))
result = (T)conv.ConvertFrom(value.AttemptedValue); // Convert the value
return result;
}
}
// Return the default of the generic type if the field name doesn't exist.
return default(T);
}
public static void NoPermissions(ActionExecutingContext filterContext)
{
// Override the result so it returns another action (No permissions action)
filterContext.Result = new RedirectResult("~/Error/NoPermissions");
}
}
<强>用法强>
[ValidateAssembly]
public JsonResult DatatableRequest(string className)
{
Type.GetType(className);
//more stuff + response
}