我遇到了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以便我可以实现可维护性和可测试性?
答案 0 :(得分:1)
Hocp模块不太可能由ioc容器解析,但是,如果您重构代码以使用身份验证过滤器而不是http模块,则应该能够设置mvc内部服务定位器来解析过滤器。 / p>
看看Rick Strahl的本教程
http://www.west-wind.com/weblog/posts/2013/Apr/18/A-WebAPI-Basic-Authentication-Authorization-Filter
和IFilterProvider示例
Property Injection into Web API's `System.Web.Http.Filters.ActionFilterAttribute`
答案 1 :(得分:0)
感谢Wiktor,我能够将实际工作的代码粘合在一起。以下是未来帮助的相关代码。其他课程在Wiktors&#39;链接。
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);
}
}