当经过身份验证的用户没有权限时,Asp.net MVC2会重定向到response 302
的登录页面。
我想分成两个动作
有什么办法吗?或者我在授权和表单身份验证方面做错了什么?我能想到的唯一方法是编写自定义授权属性,我想避免这种情况。
答案 0 :(得分:19)
您可以编写自定义过滤器属性,如下所示:
public class CustomAuthorizeAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.User.Identity == null || !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" +
filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl));
}
//Check user right here
if (userNotRight)
{
filterContext.HttpContext.Response.StatusCode = 302;
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
并在控制器中使用它:
[CustomAuthorize]
public class HomeController : Controller
{
}
答案 1 :(得分:9)
您可以编写自定义授权属性,如果用户未经过身份验证,则可以使用AuthorizeCore方法返回HttpUnauthorizedResult,如果他已通过身份验证但未在角色中执行您想要的其他操作。请注意,如果您返回401状态代码,FormsAuthentication框架最终将使用302重定向到登录页面。
答案 2 :(得分:7)
正如Customizing authorization in ASP.NET MVC中所建议的那样,您可以继承AuthorizeAttribute以拦截经过身份验证但未经授权的方案,并使用重定向替换结果。
答案 3 :(得分:7)
实施自定义AuthorizeAttribute
并添加以下覆盖。基础是检查用户是否经过身份验证但未经授权,然后重定向到您自己的“拒绝访问”页面。希望这有帮助!
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));
// Check all authorzation attributes
foreach (var attribute in attributes)
{
var authAttribute = attribute as AuthorizeAttribute;
if (authAttribute != null)
{
if (!filterContext.HttpContext.User.IsInRole(authAttribute.Roles))
{
// User is not authorized so redirect to our access denied error page
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "area", "" },
{ "controller", "Error" },
{ "action", "AccessDenied" }
});
break;
}
}
}
}
}
答案 4 :(得分:3)
与@hellangle和@Andreas建议的解决方案类似,我使用以下代码来解决此问题:
public class CustomizedAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var userAuthInfo = GetUserAuthInfo();
if (!userAuthInfo.IsAuthenticated())
{
filterContext.Result = new RedirectResult(UrlToYourLoginPage);
return;
}
if (!userAuthInfo.IsAuthorized())
{
var result = new ViewResult {ViewName = "UnAuthorized"};
result.ViewBag.Message = "Sorry! You are not authorized to do this!";
filterContext.Result = result;
}
}
}
当然,您需要根据您的特定需求实现用户授权信息类和相关方法(GetUserAuthInfo,IsAuthenticated,IsAuthorized)。另外一个名为'UnAuthorized'的视图应放在MVC引擎可以找到的地方。然后它可以在控制器类(在@ hellangle的答案中指出)或动作方法上使用:
[CustomizedAuthorizeAttribute]
public class TargetController : Controller
{
[CustomizedAuthorizeAttribute]
public ActionResult TargetAction()
{
// Your Code
}
}
为了为各种控制器类和操作方法提供不同的访问控制策略,实现CustomizedAuthorizeAttribute类的构造函数,该类接受表示访问控制信息的参数,然后相应地实例化CustomizedAuthorizeAttribute类。