ちょっと、私はFilterAttributeでプロパティインジェクションを使用することができましたが、代わりにコンストラクタに移すことが可能かどうか疑問です。ASP.NET MVC3 + ActionFilterAttribute + Injection?
私の現在のコード:事前に
// AuthAttribute.cs
public class AuthAttribute : ActionFilterAttribute
{
public Roles _authRoles { get; private set; }
[Inject]
private readonly IAuthorizationService _service;
public AuthAttribute(Roles roles)
{
_authRoles = roles;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
string redirectUrl = string.Format("?returnUrl={0}", redirectOnSuccess);
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
else
{
bool isAuthorized = _service.Authorize(GetUserSession.Id, _authRoles.ToString());
if (!isAuthorized)
{
// TODO: Make custom "Not Authorized" error page.
throw new UnauthorizedAccessException("No access");
}
}
}
}
// TestController.cs
[Auth(Roles.Developer)]
public ActionResult Index()
{
// Some smart logic
}
ありがとう!
属性のみを使用すると、CloudlessSkyが正しく機能します。フィルタはコンストラクタインジェクションを使用できますが、フィルタに属性をバインドする必要があります。私は以下の答えを提示しました。 Remo Gloor、B Z、および他の人たちのおかげで助けになりました。 –