【问题标题】:How to use custom Roles using Windows Authentication in MVC Application如何在 MVC 应用程序中使用 Windows 身份验证来使用自定义角色
【发布时间】:2017-06-27 14:26:58
【问题描述】:

我正在使用 Windows 身份验证 (WA) 开发内部 MVC 应用程序。然而,使用 WA 对用户进行身份验证是直截了当的;关于用户角色,我有以下要求:

  1. 我们将使用忽略 AD 角色的自定义角色。例如,一个用户 可能在 AD 中具有“经理”角色,但他的应用角色设置为 '导师'。用户通过身份验证后,系统将获取 用户角色并相应地设置 CurrentPrincipal。

  2. 对于以上内容,我计划有 3 个表,包括 User、Role 和用户角色。 Role 表具有自定义角色,而 User 表由公司用户组成。 UserRole 表将定义 用户与其角色之间的映射。我用这种方法看到的问题 是预填充所有 3 个表。用户表必须有所有的列表 公司员工,并为新/非在职员工维护。用户角色 应该在每个用户和他的角色登录之前设置表。

  3. 在应用程序中,用户被分配到不同的任务(例如 John 是 监督车辆)加上我们需要维护用户活动日志。假设 以上两点有效,在User中使用ID字段是否可以 用于此目的的表?

  4. 还有可能稍后我们可能会部署应用程序 在公共领域。在这种情况下,我们如何使用现有的 用于此目的的用户/角色基础架构。

提前致谢。

【问题讨论】:

    标签: windows-authentication asp.net-mvc-5


    【解决方案1】:

    我的朋友,你和我在同一条船上!我设法通过自定义授权属性做到了这一点。以下是我在这个过程中偶然发现的几点。

    我没有创建自己的用户表。您可以,但您可以根据域中的用户数量查询 AD 用户,并使用 Guid 将其链接到您的角色/活动表进行搜索。如果您确实创建了一个反映 AD 的用户表,请使用该用户的 Guid。这样,如果登录名/名称/其他任何内容发生变化,Guid 将保持不变。

    自定义授权属性:

    namespace YourSite.Attributes
    {   
        [AttributeUsage(AttributeTargets.Method)]
        public class AuthRoleAttribute : ActionFilterAttribute
        {
            public string RoleName { get; set; }
    
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (!ViewIsAuthorizedActivity(RoleName)) // create a bool function to see if your user is in this role... check your db or whatever
                {
                    string requestMethod = filterContext.HttpContext.Request.HttpMethod;
    
                    if (requestMethod == "GET")// I chose two different means of forbidding a user... this just hides the part of the page based on the @if(ViewBag.Authorization = "FORBIDDEN") where you render a partial, else show the view 
                    {
                        filterContext.Controller.ViewBag.Authorization = "FORBIDDEN";
                    }
                    else if (requestMethod == "POST") // This prevents over posting by redirecting them completely away from that controller... also prevents them from posting if they have the page loaded and you remove permission
                    {                                 // Hiding a part of the page doesn't matter for the POST if the page is already loaded
                        filterContext.Result = new RedirectToRouteResult(
                            new RouteValueDictionary
                                {
                                    { "controller", "Home" },
                                    { "action", "Forbidden" },
                                    { "area", ""}
                                });
                    }
    
                    base.OnActionExecuting(filterContext);
                }
            }           
        }
    }
    

    视图中如何处理 GET:

    @if (ViewBag.Authorization == "FORBIDDEN")
    {
        ViewBag.Title = "Forbidden!";
        @Html.Partial("~/Views/Forbidden.cshtml");
    }
    else
    <!-- User is not forbidden and have the view here -->
    

    请注意,对于 POST,用户会从控制器重定向到 Forbidden 控制器。

    控制器上的属性:

    [AuthRole(RoleName = "Admin")]
    public ActionResult YourController()
    

    我还对用户进行了扩展,因此如果他们没有权限,某些内容可能会隐藏在视图中:

    public static bool IsAuthorized(this IPrincipal user, string roleName)
        {
            return Attributes.AuthActivityAttribute.ViewIsAuthorizedByRole(roleName); // function determining if the user is in that role, therefore show what you want to show in the view or don't show it if false
        }
    

    调用者:

    @if (User.IsAuthorized("Admin"))
    {
    <!-- show something, a link, etc. -->
    }
    

    希望这能让您比我有一个更好的开端。如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多