代码之家  ›  专栏  ›  技术社区  ›  LiamB

aspnet mvc-对视图应用安全性?

  •  2
  • LiamB  · 技术社区  · 15 年前

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

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

    网站正在使用表单验证-正在尝试使此项对MVC有效 http://beensoft.blogspot.com/2008/06/mixing-forms-and-windows-authentication.html

    谢谢

    2 回复  |  直到 15 年前
        1
  •  5
  •   J. Steen    15 年前

    可以对名为authorizeAttribute的操作方法使用安全属性。

    例如,

    [Authorize(Roles = "Domain Users")]
    public ActionResult Create(FormCollection collection)
    

    为了限制对链接或类似链接的访问,甚至对用户隐藏它们,我们实现了一个名为SecurityTrimmedActionLink的扩展方法,我们主要是从 http://www.inq.me/post/ASPNet-MVC-Extension-method-to-create-a-Security-Aware-HtmlActionLink.aspx .

    public static string SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkText, string action, object routeValues)
    {
        if (IsAccessibleToUser(action, htmlHelper.ViewContext.Controller))
        {
            return htmlHelper.ActionLink(linkText, action, routeValues);
        }
    
        return string.Empty;
    }
    
    public static string SecurityTrimmedActionLink(this HtmlHelper htmlHelper, string linkText, string action)
    {
        return SecurityTrimmedActionLink(htmlHelper, linkText, action, null);
    }
    
    private static bool IsAccessibleToUser(string action, ControllerBase controller)
    {
        ArrayList controllerAttributes = new ArrayList(controller.GetType().GetCustomAttributes(typeof(AuthorizeAttribute), true));
        ArrayList actionAttributes = new ArrayList();
        MethodInfo[] methods = controller.GetType().GetMethods();
        foreach (MethodInfo method in methods)
        {
            object[] attributes = method.GetCustomAttributes(typeof(ActionNameAttribute), true);
            if ((attributes.Length == 0 && method.Name == action) || (attributes.Length > 0 && ((ActionNameAttribute)attributes[0]).Name == action))
            {
                actionAttributes.AddRange(method.GetCustomAttributes(typeof(AuthorizeAttribute), true));
            }
        }
        if (controllerAttributes.Count == 0 && actionAttributes.Count == 0)
            return true;
    
        IPrincipal principal = HttpContext.Current.User;
        string roles = "";
        string users = "";
        if (controllerAttributes.Count > 0)
        {
            AuthorizeAttribute attribute = controllerAttributes[0] as AuthorizeAttribute;
            roles += attribute.Roles;
            users += attribute.Users;
        }
        if (actionAttributes.Count > 0)
        {
            AuthorizeAttribute attribute = actionAttributes[0] as AuthorizeAttribute;
            roles += attribute.Roles;
            users += attribute.Users;
        }
    
        if (string.IsNullOrEmpty(roles) && string.IsNullOrEmpty(users) && principal.Identity.IsAuthenticated)
            return true;
    
        string[] roleArray = roles.Split(',');
        string[] usersArray = users.Split(',');
        foreach (string role in roleArray)
        {
            if (role == "*" || principal.IsInRole(role))
                return true;
        }
        foreach (string user in usersArray)
        {
            if (user == "*" || (principal.Identity.Name.Equals(user, StringComparison.InvariantCultureIgnoreCase)))
                return true;
        }
        return false;
    }
    
        2
  •  1
  •   Çağdaş Tekin    15 年前

    由于站点已经在使用表单身份验证,您将无法使用 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;
    }
    

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