我为我的问题找到的解决方案是用这个简单的 IFilterAttribute 解决的。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple=true)]
public class RedirectOnClaimFilter : ActionFilterAttribute, IActionFilter
{
public string ClaimType { get; set; }
public string ClaimValue { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
#region IActionFilter Members
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
ClaimsIdentity id = (ClaimsIdentity)filterContext.HttpContext.User.Identity;
if (id.HasClaim(p => p.Type == ClaimType && string.Compare(p.Value, ClaimValue, true) == 0))
filterContext.Result = RedirectToAction(Controller, Action);
base.OnResultExecuting(filterContext);
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
//throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
ClaimsIdentity id = (ClaimsIdentity)filterContext.HttpContext.User.Identity;
if (id.HasClaim(p => p.Type == ClaimType && string.Compare(p.Value, ClaimValue, true) == 0))
filterContext.Result = RedirectToAction(Controller, Action);
}
private ActionResult RedirectToAction(string Controller, string Action)
{
return new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary()
{
{"Controller", Controller},
{ "Action", Action}
});
}
#endregion
}
然后我可以选择一个特定的声明来验证 Controller 调用,如下所示:
[RedirectOnClaimFilter(Action="WaitForApproval",
Controller="Account",
ClaimType = "http://solution/claims/pendinguser",
ClaimValue="true")]
[RedirectOnClaimFilter(Action = "RegisterFederated",
Controller = "Account",
ClaimType = "http://solution/claims/newuser",
ClaimValue = "true")]
[HandleError(ExceptionType = typeof(SecurityException), View = "Error")]
public class OrganizationController : BaseController
{
public ActionResult OrganizationAction()
{
return View();
}
}
它将检查委托人的声明集中的特定声明,如果找到,用户将被重定向到特定的控制器和操作。