0

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);

    }


}
Steven
  • 166,672
  • 24
  • 332
  • 435
mohammadreza
  • 158
  • 12
  • So what's the exception? Simple Injector will always throw an exception with a very clear exception message. Without this message, it's very hard for us to help you. – Steven Jul 18 '17 at 10:53
  • @Steven, unfortunately there is no Exception from Simpleinjector, its just return **The resource cannot be found** – mohammadreza Jul 18 '17 at 11:11
  • There is _always_ an exception. Set a break point in Visual Studio, or set "break on all exceptions" in Visual Studio, or log exceptions to disk. There must be an exception. – Steven Jul 18 '17 at 12:11
  • @Steven trust me, there is no Exception in my project :(( - the error page just shown after all methods ends, i can send my project to you its empty at all, i don't know what i missed :( – mohammadreza Jul 18 '17 at 14:20
  • In that case the problem is not with Simple Injector or your SI configuration. My advise would be to start a clean project and add stuff in little steps to find when the problem appears. Might be something with your url mapping for instance. – Steven Jul 18 '17 at 14:25
  • 1
    @Steven, i starts again, i found that the problem is owin mapping – mohammadreza Jul 19 '17 at 07:17

1 Answers1

1

After challenging with my project and left it for a 2 week I found the solution for all of my problems, Thank you @Steven

The main problem was owin mapping.

I must remove api segment from route defintion, I don't know why yet, but it works. thanks @Tratcher

owin map extension return 404

mohammadreza
  • 158
  • 12