我正在尝试在我的MVC应用程序中设置自定义身份验证过滤器,但我需要在身份验证过滤器中引用我的用户服务。我像Unity一样设置我的服务:
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
public static void RegisterTypes(IUnityContainer container)
{
// container.LoadConfiguration();
// TODO: Register your types here
container.RegisterType<IUserService, UserService>();
}
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(GERPWeb.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(GERPWeb.App_Start.UnityWebActivator), "Shutdown")]
namespace APP.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
这种方法在我可以拥有构造函数的任何地方都可以正常工作,如下所示:
public class UserController : BaseController
{
private readonly IUserService _userService;
public CustomerController(IUserService userService)
{
_userService = userService;
}
但是我的身份验证过滤器上没有一个:
public class BasicAuthorization : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//see if we can skip the authorization
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization)
{
//var userService = ServiceLocator.Current.GetInstance<IUserService>();
//get instance of user service here
base.OnAuthorization(filterContext);
}
}
}
然后在我的FilterConfig.cs中:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new BasicAuthorization());
filters.Add(new HandleErrorAttribute());
}
我找到了一个名为服务定位器的引用,并试图尽我所能地使用它,但我想我错过了一些内容,因为我在该行上获得ServiceLocationProvider must be set.
,并且所有谷歌链接适用于WPF。
我在这里缺少什么?
答案 0 :(得分:2)
您已经设置了DependencyResolver
,它基本上遵循服务定位器模式。
public class BasicAuthorization : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
//see if we can skip the authorization
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization) {
//Using dependency resolver here
var userService = (IUserService) DependencyResolver.Current.GetService(typeof(IUserService));
base.OnAuthorization(filterContext);
}
}
}
您可以创建一个扩展方法,以便更清晰地解析实例
public static T GetService<T>(this IDependencyResolver container) {
return (T)container.GetService(typeof(T));
}
哪个允许
var userService = DependencyResolver.Current.GetService<IUserService>();