什麼,你需要做的是讓你的CustomAuthorize屬性繼承Microsoft.Practices.Unity.InterceptionExtension.HandlerAttribute 和 覆蓋「ICallHandler CreateHandler(IUnityContainer容器)」方法。
public class CustomAuthorizeAttribute: HandlerAttribute
{
public IAuthorizeAttributeHandler AuthorizationHandler { get; set; }
public override ICallHandler CreateHandler(IUnityContainer container)
{
AuthorizationHandler= new AuthorizationAttributeHandler
{
DBContext = container.Resolve<IDBContext>()
};
return AuthorizationHandler;
}
}
現在創建從Microsoft.Practices.Unity.InterceptionExtension.ICallHandler派生接口並添加IDBContext 作爲成員。
public interface IAuthorizeAttributeHandler : ICallHandler
{
IDBContext DBContext;
}
IAuthorizationAttributeHandler實施
public class AuthorizationAttributeHandler : IAuthorizeAttributeHandler
{
public IDBContext DBContext
{
get; set;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
var result = DBContext.DoWork(input.Arguments..);
//// Invoke the handler
IMethodReturn output = getNext()(input, getNext);
return getNext()(input, getNext);
}
}
要在統一配置添加簡單的攔截擴展。
unityContainer
.AddNewExtension<Interception>()
.Configure<Interception>()
.SetInterceptorFor<IYourPageInterface>(new InterfaceInterceptor());
將屬性添加到希望執行方面的接口方法。
[CustomAuthorize]
ActionResult Index()
{
}
希望這有助於 乾杯 斯廷