我当前的代码:
// AuthAttribute.cspublic 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}
提前致谢!
解决方法 不,这不可能作为构建器 must be simple types的参数.为了测试的目的,您可以使用另一个构造函数(因为您不应该在测试中使用IoC容器):
public class AuthAttribute : ActionFilterattribute{ public Roles _authRoles { get; private set; } [Inject] private Readonly IAuthorizationService _service; public AuthAttribute(Roles roles) { _authRoles = roles; } public AuthAttribute(Roles roles,IAuthorizationService authSvc) : this(roles) { this.service = authSvc; } // ...}总结
以上是内存溢出为你收集整理的c# – ASP.NET MVC3 ActionFilterAttribute注入?全部内容,希望文章能够帮你解决c# – ASP.NET MVC3 ActionFilterAttribute注入?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)