gpt4 book ai didi

c# - 使用 Unity 2 和 MVC 3 将类注入(inject)身份验证属性

转载 作者:太空狗 更新时间:2023-10-29 21:51:32 26 4
gpt4 key购买 nike

我已经阅读了很多指出如何做到这一点的文章,即:

Stack overflow solution

Brad Wilsons excellent tutorial

这些看起来确实运作良好,但是当我遵循此处的一些指南时

Securing your ASP.NET MVC 3 Application

我好像来了个收割机。对我来说,问题是当我将我的 AuthorizationAttribute 添加为 GlobalFilter 而不是仅仅装饰 Controller /操作时。尽管依赖解析器似乎被调用并设置了我公开的 [Dependancy] 属性,但当它实际涉及到我覆盖 AuthorizeAttribute 的 OnAuthorization() 方法的代码部分时,我的公共(public) [Dependency] 属性为空。

当我将其从全局过滤器中移除并装饰 Controller 时,它似乎可以正常工作。

如果需要更多信息,我可以发布代码。

编辑:为了进一步扩展这里是我的一些代码:

全局.asax.cs

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// If I have this here the my [Dependency] attribute does not keep it's value
// If I remove it and decorate the controller the [Dependency] attribute keeps it value
filters.Add(new BasicAuthenticationAttribute());
filters.Add(new HandleErrorAttribute());
}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

// injection of services and data contexts
var container = new UnityContainer();

// injections for services
RegisterUnityServices(container);

// Filter provides to allow injection into attribute filters
RegisterUnityFilters(container);

}

private void RegisterUnityServices(UnityContainer container)
{
container.RegisterType<IDataContext, CDAXDataContext>();

container.RegisterType<IUploadService, UploadService>();
container.RegisterType<IAuthenticationService, AuthenticationService>();

// add more services here ...
DependencyResolver.SetResolver(new Models.UnityDependencyResolver(container));
}

private void RegisterUnityFilters(UnityContainer container)
{
var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
FilterProviders.Providers.Remove(oldProvider);

var provider = new UnityFilterAttributeFilterProvider(container);
FilterProviders.Providers.Add(provider);
}

我的 Unity 类取自布拉德·威尔逊的例子:

    public class UnityDependencyResolver : IDependencyResolver
{
readonly IUnityContainer _container;

public UnityDependencyResolver(IUnityContainer container)
{
this._container = container;
}

public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
catch
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}
catch
{
return new List<object>();
}
}
}

    public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider 
{
private IUnityContainer _container;

public UnityFilterAttributeFilterProvider(IUnityContainer container)
{
_container = container;
}

public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var filters = base.GetFilters(controllerContext, actionDescriptor);

foreach (var filter in filters)
{
_container.BuildUp(filter.Instance);
}

return filters;
}

protected override IEnumerable<FilterAttribute> GetControllerAttributes(
ControllerContext controllerContext,
ActionDescriptor actionDescriptor)
{

var attributes = base.GetControllerAttributes(controllerContext,
actionDescriptor);
foreach (var attribute in attributes)
{
_container.BuildUp(attribute.GetType(), attribute);
}

return attributes;
}

protected override IEnumerable<FilterAttribute> GetActionAttributes(
ControllerContext controllerContext,
ActionDescriptor actionDescriptor)
{

var attributes = base.GetActionAttributes(controllerContext,
actionDescriptor);
foreach (var attribute in attributes)
{
_container.BuildUp(attribute.GetType(), attribute);
}

return attributes;
}
}

以及我的 BasicAuthenticationAttribute 的基础,其中我设置了 [Dependency] 属性,但在触发 OnAuthorization 方法时不维护该值。

    public class BasicAuthenticationAttribute : AuthorizeAttribute
{
[Dependency]
public IAuthenticationService Authentication { get; set; }

private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
{
validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
}

public override void OnAuthorization(AuthorizationContext filterContext)
{
// code here that uses the Authentication attribute but it's null
}

最佳答案

我认为您需要在属性类中调用 DependencyResolver...不确定 [Dependency] 是否属性在这里工作...所以在 OnAuthorization方法调用 IAuthenticationService authentication = DependencyResolver.Current.GetService<IAuthenticationService>();

关于c# - 使用 Unity 2 和 MVC 3 将类注入(inject)身份验证属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com