C# 向受域登录/角色限制的网页添加功能
我目前正在做一个简单的小项目。我有一个存储在数据库中的用户列表,如下所示: Id(uniqueidentifier主键)、FirstName(varchar)、LastName(varchar)、PhoneNo(varchar)、DomainAC(varchar) 我在这样的网页上显示他们的信息:C# 向受域登录/角色限制的网页添加功能,c#,asp.net-mvc-2,C#,Asp.net Mvc 2,我目前正在做一个简单的小项目。我有一个存储在数据库中的用户列表,如下所示: Id(uniqueidentifier主键)、FirstName(varchar)、LastName(varchar)、PhoneNo(varchar)、DomainAC(varchar) 我在这样的网页上显示他们的信息: <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<WhoIs.Models.Employee&
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<WhoIs.Models.Employee>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
<table>
<tr>
<th>
<%: Html.DisplayNameFor(model => model.FullName) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.PhoneNo) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.DomainAC) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Branch) %>
</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.FullName) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.PhoneNo) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.DomainAC) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Branch) %>
</td>
<td>
<% if (User.IsInRole(@"Admin") || User.Identity.Name == item.DomainAC) {%>
<%: Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
<% } %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
有谁能简单地向我解释一下我需要做什么才能使这项工作成功吗?我之前发布了一个类似的问题,它真的超出了我的头脑-谈论ActionController等,没有解释它们在您的项目中的位置/它们如何链接在一起/如何使用
有人能帮我吗?我将在两天内对这件事给予一点奖励,并将其授予最佳答案,因为这是最近我经常遇到的事情!谢谢。答案并不复杂,只是取决于你想如何实现它。(我假设您正在使用MVC,这是因为其中一些是如何实现的,以及您是如何对其进行标记的) 在控制器中,您需要确定用户是否已登录,以及该数据。好吧,假设你有
public ActionResult LoadUserData(){
var model = new LoadUserDataModel();
// code here to populate your model
var isUserAuthenticated = MethodToFigureOutIfUserIsAuthenticated(); // returns bool
ViewBag.isUserAuthenticated = isUserAuthenticated; // I don't like using the ViewBag so you can add this to the model if you wish.
return View(model);
}
我不想撒谎,对那些看这个的专家来说,他们可能有点害怕。我理解。一个方法属性会更有效,但我在这里试图帮助这个家伙
现在在视图上(我刚刚复制了你的并做了一些调整)
指数
型号(全名)%%>
型号。电话号码)%%>
model.DomainAC)%%>
型号(分支机构)%>
项目(全名)%>
项目。电话号码)%%>
item.DomainAC)%>
项目(分支机构)%>
|
而且。。。应该对你有用
回到你问题的另一部分。你想知道这一切是如何联系在一起的。这个答案在一个简单的问答论坛上很难回答。所以我将简要概述这些角色是什么
MVC代表模型/视图/控制器。它是一种分离应用程序开发关注点的编程方法。基本上,控制器是应用程序的数据/获取器/抓取器/保存器。对于业务应用程序开发有一个完整的理解水平,这在一所主要大学不到4年的时间里是无法解释的,但我们可以说这是应用程序的核心内容。视图是应用程序的表示形式。也称为用户界面。该部分控制应用程序的用户输入和实际操作。模型是把它们连接在一起的粘合剂。控制器将获取填充模型所需的数据,然后将模型传递给视图,然后视图将使用它
为什么这是必要的?如果您想开发可维护的应用程序和可重用的代码,这不仅是必要的,而且是绝对必要的。但这也是MVC的魅力所在
我希望这回答了你的问题。如果您需要更多的澄清,请让我知道,我会相应地更新我的答案
我还建议你去这个网站,向右看,你会看到一个部分,上面写着Pluralsight和一个关于MVC4的网络系列。当我开始使用MVC时,我发现这非常有用。您可以检查控制器本身中的用户角色权限,基于此,您可以将响应重定向到网页
您可以使用两个页面列出所有用户,另一个页面使用“创建”按钮。在基于用户角色的控制器中,您可以将响应重定向到相关页面。这可以通过两种方式处理
最简单、最可扩展和可维护的方法是在上使用扩展方法 但是,您的问题中似乎有相互冲突的信息 if(User.IsInRole(@“Admin”)| | User.Identity.Name==item.DomainAC) 似乎与逻辑不一样 如果(DomainAC==当前登录域AC | |当前登录域AC为admin) 所以我不确定这个代码是不是你想要的 WebViewPage可以通过访问当前登录的用户作为
IPrincipal
namespace MyProject.<whatever>
{
public static IPrincipalExtensions
{
public bool IsAdminOrDomainAC(this IPrincipal instance, string name)
{
if (instance == null
|| instance.Identity == null
|| !instance.Identity.IsAuthenticated)
{
return false;
}
return instance.IsInRole(@"Admin")
|| instance.Identity.Name == name;
}
}
}
名称空间MyProject。
{
公共静态IP原则
{
public bool isAdminNordominac(此IPrincipal实例,字符串名称)
{
如果(实例==null
||instance.Identity==null
||!instance.Identity.IsAuthenticated)
{
返回false;
}
返回instance.IsInRole(@“Admin”)
||instance.Identity.Name==名称;
}
}
}
现在,您的视图代码如下所示:
<td>
<% if this.User.IsAdminOrDomainAc(item.DomainAC) {%>
<%: Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
<% } %>
</td>
|
您甚至可以在视图目录中编辑web.config文件并添加
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyProject.<whatever>" />
使扩展方法可用于views文件夹中的所有视图,而无需在每个视图的顶部使用using MyProject.
这假设您返回多个记录,并且用户只有编辑/删除其中一些记录的权限。您还需要确保在“编辑/删除”方法中,用户可以访问加载的项。因为它是IPrin的一个扩展方法
<td>
<% if this.User.IsAdminOrDomainAc(item.DomainAC) {%>
<%: Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
<% } %>
</td>
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyProject.<whatever>" />
using MyProject.<whatever>
public class MyController
{
public ActionResult Edit(int id)
{
var item= DB.GetRow(id) //whatever that looks like
if (!this.User.IsAdminOrDomainAc(item.DomainAC))
{
return this.RedirecToAction("Index");
}
return this.View(item);
}