Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc AspNet MVC-对视图应用安全性?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc AspNet MVC-对视图应用安全性?

Asp.net mvc AspNet MVC-对视图应用安全性?,asp.net-mvc,Asp.net Mvc,我想在我的一个视图中添加集成的windows安全性,这可能吗 在web表单上,我只需在IIS中找到文件,并在其中添加文件安全功能,obv MVC是非基于文件的,这似乎不起作用 该网站正在使用Forms Auth-试图使其适用于MVC 谢谢您可以在名为AuthorizeAttribute的操作方法上使用安全属性 比如说, [Authorize(Roles = "Domain Users")] public ActionResult Create(FormCollection collection)

我想在我的一个视图中添加集成的windows安全性,这可能吗

在web表单上,我只需在IIS中找到文件,并在其中添加文件安全功能,obv MVC是非基于文件的,这似乎不起作用

该网站正在使用Forms Auth-试图使其适用于MVC


谢谢

您可以在名为AuthorizeAttribute的操作方法上使用安全属性

比如说,

[Authorize(Roles = "Domain Users")]
public ActionResult Create(FormCollection collection)
为了限制对链接或类似链接的访问,甚至对用户隐藏它们,我们实现了一个扩展方法SecurityTrimmedActionLink,这是我们主要采用/借用的方法


由于站点已在使用表单身份验证,您将无法使用控制器/操作上的
Authorize
属性检查角色或名称。因为这将使用当前提供程序(表单),而不是Windows

一个快速且不那么优雅的解决方案是使用下面的函数,并在返回视图之前对其进行检查

bool IsWindowsAuthenticated() {
    //the following classes are under System.Security.Principal
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.Identity.IsAuthenticated;
}

请注意,可能有更好的方法来做到这一点。我只是举个例子,以防万一有用。

是的,这很好,但在我们的例子中,我们没有设定角色。我试图做的是将集成的Windows安全性添加到几个视图中(站点的其余部分是form auth)-使用可以设置授权(Users=“Pete User”)以及。。。查找使用AuthorizeAttribute的其他示例。=)你甚至可以把它放在[Authorize]上,让该方法知道你只是想要一个授权用户。这很好,但这无助于混合身份验证模式?也许你也可以看看这个:我认为如果你编辑这个问题并说该网站已经在使用forms authentication.Nice会更好。我忘了我在发布的方法中只使用了HttpContext用户主体。=)@J.Steen,我本来打算在评论中解释的,但后来我觉得答案会更好。
bool IsWindowsAuthenticated() {
    //the following classes are under System.Security.Principal
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.Identity.IsAuthenticated;
}