When i use Simpleinjector beside owin OAUth for Token Based Login, Simpleinjector cannot resolve my controllers and i got a 404 error with the "The resource cannot be found" message.
i use owin pipeline for my requests and Owin Security to Generate and validate user tokens.
Simpleinjector as DI and ASP.NET 5
i tried this solutions on stackoverflow: UserManager dependency injection with Owin and Simple Injector
and this one at simpleinjector documentations: http://simpleinjector.readthedocs.io/en/latest/owinintegration.html
and this one dose not work for me (i do not have BeginExecutionContextScope
)
WebApi + Simple Injector + OWIN
this is my startup.cs:
public class Startup
{
public void Configuration(IAppBuilder app)
{
SimpleInjectorBootstrapper.Initialize();
app.Use(async (context, next) => {
using (AsyncScopedLifestyle.BeginScope(SharedLayer.Core.CoreObject.container))
{
await next();
}
});
ConfigureOAuth(app);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(SharedLayer.Core.CoreObject.container));
config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(SharedLayer.Core.CoreObject.container);
app.Use<ExceptionMiddleware>();
app.Map(new PathString("/api"), application => { application.Use<AuthenticateMiddleware>(); });
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new AuthorizationProvider(),
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
and this is my simpleinjector bootstrapper:
public static class SimpleInjectorBootstrapper
{
public static void Initialize()
{
// Create a new Simple Injector container
var container = new Container();
// Set default Scope
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
// Configure the container (register)
var businessAssembly = typeof(BusinessLayer.Bootstrap.SimpleInjectorBootstrapper).Assembly;
// get services based on conditions
var registerations = from type in businessAssembly.GetExportedTypes()
where type.Namespace.Contains("BusinessLayer.Logic")
where type.GetInterfaces().Any()
select new
{
Service = type.GetInterfaces().First(),
Implementation = type
};
// register each service
foreach (var reg in registerations)
{
container.Register(reg.Service, reg.Implementation, Lifestyle.Scoped);
}
// init nested bootstrapper
BusinessLayer.Bootstrap.SimpleInjectorBootstrapper.Initialize(container);
// Register Root services
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.RegisterMvcIntegratedFilterProvider();
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
// Optionally verify the container's configuration.
container.Verify();
// Store the container for use by the application
//DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
// Assign SimpleInjectorWebApiDependencyResolver to DependencyResolver
//GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
// store container in a static variable for DI in class libraries
SharedLayer.Core.CoreObject.container = container;
}
}
when i comment this two lines, my application works fine:
ConfigureOAuth(app);
app.Map(new PathString("/api"), application => { application.Use<AuthenticateMiddleware>(); });
where is my mistake?
Edit:
I found that when i comment this line my project works fine:
app.Map(new PathString("/api"), application => {
application.Use<AuthenticateMiddleware>();
});
and here is my AuthenticateMiddleware Code:
public class AuthenticateMiddleware : OwinMiddleware
{
public AuthenticateMiddleware(OwinMiddleware next)
: base(next)
{}
public async override Task Invoke(IOwinContext context)
{
string username = "";
if (context.Request.Method.Equals(HttpMethod.Get.Method))
{
if (context.Request.Query.Any(e => e.Key == "username" && e.Value != null))
username = context.Request.Query.Get("username");
}
else
{
var body = new StreamReader(context.Request.Body).ReadToEndAsync().Result;
var dict = HttpUtility.ParseQueryString(body);
var json = new JavaScriptSerializer().Serialize(dict.AllKeys.Where(e => e.Equals("username")).ToDictionary(k => k, k => dict[k]));
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(json);
username = jsonObject["username"] != null ? jsonObject["username"].ToString() : "";
}
if (username == "")
throw new ArgumentNullException("username is required.");
if (!context.Authentication.User.HasClaim("username", username))
throw new SecurityException("token is invalid.");
await Next.Invoke(context);
}
}