【发布时间】:2019-11-09 08:42:56
【问题描述】:
我正在开发包含安全性的自定义控件。当用户无权访问该控件时,该控件会使其自身禁用但也将变为不可见。该控件未呈现,也未出现在页面上。在这一点上,一切都很好。
我的问题是如何保护控件以防止用户更改值?
我已经使用 Chrome HTML Inspector 在我的表单中注入了一个输入,因为就像它应该的那样,该字段没有呈现,当我提交带有新值的注入输入的表单时,服务器在控件中具有新值价值属性。
public enum UserRole {
Standard,
Administrator,
[...]
}
//For this example, my custom control is derived from HtmlInputText. [ToolboxData("<{0}:MyCustomControl runat=\"server\"></{0}:MyCustomControl>")]
public class MyCustomControl: System.Web.UI.HtmlControls.HtmlInputText
{
public UserRole? MinimumRoleRequired { get; set; }
protected override void OnLoad(EventArgs e)
{
//Simplified version
if (this.Page.CurrentUser.Role < this.MinimumRoleRequired)
{
this.Visible = false;
this.Disabled = true;
return;
}
[...]
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.Visible || this.Disabled)
{
return;
}
[...]
}
[...]
}
//My page who contain the control:
//HTML (MyPage.aspx)
<Controls:MyCustomControl ID="tbAdminOnly"runat="server"></Controls:MyCustomControl>
//C# (MyPage.aspx.cs)
public partial class UserEdit : Page
{
protected override void OnInit(EventArgs e)
{
this.tbAdminOnly.MinimumRoleRequired = UserRole.Administrator;
[...]
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.IsPostBack)
{
string postBackValue = tbAdminOnly.Value;
return;
}
tbAdminOnly.Value = "Hello world!";
}
}
当我以标准用户身份加载页面时,不会呈现控件。但是如果我在 html 页面中注入输入
//Note, i need to know the valid name/id but it could be done.
<input type="text" name="tbAdminOnly" id="tbAdminOnly" value="Damn shit">
postBackValue 现在是来自注入输入的新值。我怎样才能防止这种情况?
谢谢。
【问题讨论】:
标签: c# security webforms custom-controls