【问题标题】:Custom Authorize Attribute Redirect When UnAuthorized未授权时自定义授权属性重定向
【发布时间】:2013-04-21 02:31:20
【问题描述】:

在我的 C# MVC4 应用程序中,我正在执行几个不同的重定向到来自自定义授权属性内部的操作,具体取决于用户是否登录、处于某个角色等。

我已将授权属性置于我的一个操作结果之上。如果用户未登录或未通过身份验证或登录但不是我检查的任一组的成员,我希望执行操作结果中的代码。如果用户已登录并且是任一组的成员,我希望重定向到另一个操作(目前正在运行)。

使用我当前的代码,已登录并在指定组内的那些将根据需要重定向。其他类别中列出的所有内容都会导致我的 AuthorizationContext 为空。知道当这是 null HandleUnauthorizedRequest 被调用时,我试图覆盖它以允许访问原始 actionresult 但无法弄清楚。

无论我尝试什么,我都会收到错误:Object Reference not set to an instance of an object 在线:filterContext.Result = new RedirectToRouteResult(

我的授权属性代码如下:

     public class AuthorizeEditAttribute : AuthorizeAttribute
        {
            public string hName { get; set; }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);

            // Check if user is authenticated and if this action requires authorization
            if (filterContext.HttpContext.User.Identity.IsAuthenticated
                && filterContext.ActionDescriptor.IsDefined(typeof(AuthorizeAttribute), true)
                || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AuthorizeAttribute), true))
            {
                List<object> attributes = new List<object>(filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true));
                attributes.AddRange(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true));


                hName = filterContext.Controller.ValueProvider.GetValue("hSearch").AttemptedValue;


                // Check all authorzation attributes
                foreach (var attribute in attributes)
                {

                    var authAttribute = attribute as AuthorizeAttribute;
                    if (authAttribute != null)
                    {
                        if ((filterContext.HttpContext.User.IsInRole("TCL-CAdmin")) || (filterContext.HttpContext.User.IsInRole("TCL-C Group")))
                        {

                            // User is not authorized to perform edits so redirect to Index_Perm ActionResult
                            filterContext.Result = new RedirectToRouteResult(
                                new RouteValueDictionary 
                            {
                                //{ "area", "" },
                                { "controller", "Home" },
                                { "action", "Index_Perm" },
                                { "hSearch", hName.ToString() }
                            });
                            break;
                        }
                        else
                        {
                            filterContext.Result = new RedirectToRouteResult(
                               new RouteValueDictionary 
                            {
                                //{ "area", "" },
                                { "controller", "Home" },
                                { "action", "Index" },
                                { "hSearch", hName.ToString() }
                            });
                            break;
                        }
                    }
                }
            }
            else
            {

                filterContext.Result = new RedirectToRouteResult(
   new RouteValueDictionary 
                            {
                                { "controller", "Home" },
                                { "action", "Index" },
                                { "hSearch", hName.ToString() }
                            });
            }
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {

                filterContext.Result = new RedirectToRouteResult(
 new RouteValueDictionary 
                            {
                                { "controller", "Home" },
                                { "action", "Index" },
                                { "hSearch", hName.ToString() }
                            });
        }
    }
}

除了重写 HandleUnauthorizedRequest,我还尝试将 OnAuthorization 的开头部分修改为如下所示:

public override void OnAuthorization(AuthorizationContext filterContext)
        {

            base.OnAuthorization(filterContext);

            if (filterContext.Result is HttpUnauthorizedResult)
            {
                filterContext.Result = new RedirectToRouteResult(
   new RouteValueDictionary 
                            {
                                { "controller", "Home" },
                                { "action", "Index" },
                                { "hSearch", hName.ToString() }
                            });
            }

我仍然收到关于对象引用的相同警告。

【问题讨论】:

    标签: c# asp.net-mvc-4 authorization forms-authentication


    【解决方案1】:

    问题是由路径值“hSearch”引起的,它被分配了 hName 的值。在我的每次尝试中,hName 始终为 null,因为我直到以下行才设置它的值:hName = filterContext.Controller.ValueProvider.GetValue("hSearch").AttemptedValue;,它从未被击中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2014-10-28
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多