[英]Roles and User management in asp.net webforms using code behind
Issue : Using roles that I have created in my database, I want to be able to limit use for specific users depending on that users role. 问题 :使用我在数据库中创建的角色,我希望能够根据该用户角色来限制特定用户的使用。
Information : I am not using an MVC approach, only empty .aspx pages that I have wired up through to my database to show information etc. Provided are images of tables that are holding the role/user information in my database Roles , Users , Role/User . 信息 :我没有使用MVC方法,只有空的.aspx页已连接到数据库以显示信息等。所提供的表图像包含了数据库中的角色/用户信息角色 , 用户 , 角色/用户 。 Ideally I want to remove items from my menu depending on which role the user is in. So lets say the user is in the role "Worker" they will only see 2 menu "controls" or "buttons" on the aspx page menu.
理想情况下,我想根据用户所处的角色从菜单中删除项目。因此,假设用户处于“工作人员”角色,则他们只会在aspx页面菜单上看到2个菜单“控件”或“按钮”。
Attempted solution : On the landing page after a user has logged in, the Page_Load method checks to see which role the user is in and sends them to the appropriate page. 尝试的解决方案 :用户登录后在登录页面上,Page_Load方法检查用户所处的角色并将其发送到相应的页面。 The issue with this solution is that I have to create duplicates for every page depending on which role the user is in.
该解决方案的问题在于,我必须根据用户所处的角色为每个页面创建重复项。
Question : How can I edit the HTML in a aspx webform using the c# code behind. 问题 :如何使用后面的C#代码在aspx网络表单中编辑HTML。
Question 2 : Is there a simple solution to limit use depending on a user's role without have a ton of duplicate aspx pages. 问题2 :是否有一个简单的解决方案来限制使用,具体取决于用户的角色,而没有大量重复的aspx页。
Please let me know if there is more information needed. 请让我知道是否需要更多信息。
The menu is created in the HTML on the aspx form like this: 菜单是用HTML在aspx表单上创建的,如下所示:
<ul id="centered">
<li><a href='Welcome.aspx'><span>Home</span></a></li>
<li class='active has-sub'>
<a href='#'><span>Sales</span></a>
<ul>
<li class="active has-sub">
<a href='#'><span>Sales</span></a>
<ul>
<li><a href='Sales.aspx'><span>Create</span></a></li>
<li><a href='Sales.aspx'><span>View</span></a></li>
</ul>
</li>
</ul>
</li>
You need to mark you menu ul to runat="server"
and give it a unique id. 您需要将菜单ul标记为
runat="server"
并为其赋予唯一的ID。
<ul id="centered">
<li><a href='Welcome.aspx'><span>Home</span></a></li>
<li class='active has-sub'>
<a href='#'><span>Sales</span></a>
<ul>
<li class="active has-sub">
<a href='#'><span>Sales</span></a>
<ul runat="server" id="ul_menu">
<li><a href='Sales.aspx'><span>Create</span></a></li>
<li><a href='Sales.aspx'><span>View</span></a></li>
</ul>
</li>
</ul>
</li>
</ul>
Here I have change to <ul runat="server" id="ul_menu">
在这里,我更改为
<ul runat="server" id="ul_menu">
Now you can use this id ul_menu and add item to this from code behind on the basis of role of the user. 现在,您可以使用此ID ul_menu并根据用户角色从后面的代码中添加项目。 for example --
例如 -
protected void Page_Load(object sender, EventArgs e)
{
PopulateMenu();
}
private void PopulateMenu()
{
string role = "B";//You can get the role of logged in user from membeship
StringBuilder txt = new StringBuilder();
if (role == "A")
{
txt.Append("<li><a href='Sales.aspx'><span>Create</span></a></li>");
}
if (role == "B")
{
txt.Append("<li><a href='Sales.aspx'><span>View</span></a></li>");
}
ul_menu.InnerHtml = txt.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.