日期:2014-05-17  浏览次数:20369 次

自定义AuthorizeAttribute,参数有没有办法传接口的
 /// <summary>    
    /// 用户验证过滤器    
    /// </summary>    
   public class AccountAuthentication : AuthorizeAttribute
   {
       public AccountAuthentication(ICookieStorageService _cookie)
       {
           this.cookie = _cookie;
               
           ToUrl = AccountToUrlEnum.Login;
       }

ICookieStorageService _cookie
[AccountAuthentication(cookie=_cookie)]
public ActionResult About()
        {
            return View();
        }

控制器使用ioc工厂,自定义的验证需要访问ICookieStorageService ,不知道怎么传过去,或者有其他办法?
ioc class mvc asp.net 自定义attribute

------解决方案--------------------
attribute是编译的时候写进去的,没办法传变量。你其实需要重新实现一个验证过滤器,而不仅仅是attribute。
------解决方案--------------------
实现 IAuthorizationFilter

http://geekswithblogs.net/brians/archive/2010/07/08/implementing-a-custom-asp.net-mvc-authorization-filter.aspx

http://www.leadnt.com/2010/01/%E5%9C%A8asp-net-mvc%E4%B8%AD%E4%BD%BF%E7%94%A8filter%E5%81%9A%E6%9D%83%E9%99%90/

http://code.zc4u.com/articles/3038.html

http://www.cnblogs.com/DanielChow/archive/2010/05/03/1726411.html