1

我对 ASP .NET Web API 有疑问。我已按照教程http://www.asp.net/web-api/overview/security/basic-authentication并已成功将其与示例代码一起使用。但是我想将它与我所做的 AuthenticationService 绑定:

public class AuthenticationService : IAuthenticationService
{
        private readonly GenericRepository<User> _userRepository;

        public AuthenticationService(GluEntities entites)
        {
            _userRepository = new GenericRepository<User>(entites);
        }

        public bool Authenticate(string userName, string password)
        {
            var user = _userRepository.Get(x => x.UserName == userName).FirstOrDefault();
            if (user == null || user.Password != password)
            {
                return false;
            }
            return true;
        }
}

我试图将它与我的示例代码联系起来,如下所示:

public class BasicAuthHttpModule : IHttpModule
{
        private const string Realm = "GluService";
        private readonly IAuthenticationService _authenticationService;

        public BasicAuthHttpModule(IAuthenticationService authenticationService)
        {
            _authenticationService = authenticationService;
        }

        /... some irrelevant methods that are explained in the link

        private bool CheckPassword(string username, string password)
        {
            return _authenticationService.Authenticate(username, password);
        }

}

我正在尝试使用 ninject 解决 _authenticationService,但是应用程序在运行时抛出 Conustructor not found 错误。知道如何在运行时解析 _authenticationService 以便实现可维护性和可测试性吗?

4

2 回答 2

2

Http 模块不太可能被 ioc 容器解析,但是,如果您重构代码以使用身份验证过滤器而不是 http 模块,您应该能够设置 mvc 内部服务定位器来解析您的过滤器。

看看 Rick Strahl 的这个教程

http://www.west-wind.com/weblog/posts/2013/Apr/18/A-WebAPI-Basic-Authentication-Authorization-Filter

和 IFilterProvider 示例

属性注入到 Web API 的 `System.Web.Http.Filters.ActionFilterAttribute`

于 2013-11-11T20:57:15.333 回答
0

多亏了 Wiktor,我能够将一个真正有效的代码粘合在一起。这是未来帮助的相关代码。其他课程在 Wiktors 的链接中。

public class NinjectFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
    {
        private readonly IKernel _kernel;

        public NinjectFilterProvider(IKernel kernel)
        {
            _kernel = kernel;
        }

        public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
        {
            var filters = base.GetFilters(configuration, actionDescriptor);

            foreach (var filter in filters)
            {

                _kernel.Inject(filter.Instance);
            }

            return filters;
        }
    }

public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            //Support WebApi
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

            RegisterServices(kernel);
            return kernel;
        }
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            //kernel.Bind<TCountingRepository>().To<CountingRepository>();
            //kernel.Bind<CountingContext>().To<CountinContext>();
            kernel.Bind<IAuthenticationService>().To<AlwaysAcceptAuthenticationService>();

            kernel.Bind<IFilterProvider>().To<NinjectFilterProvider>();
        }
    }

这里是过滤器

public class GluAuthenticationFilter : BasicAuthenticationFilter
    {
        [Inject]
        public IAuthenticationService AuthenticationService { get; set; }

        public GluAuthenticationFilter()
        { }

        public GluAuthenticationFilter(bool active)
            : base(active)
        { }


        protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
        {
            return AuthenticationService.Authenticate(username, password);
        }
    }
于 2013-11-12T00:32:32.113 回答