当我在Owin OAUth旁边使用Simpleinjector进行基于令牌的登录时,Simpleinjector无法解析我的控制器,并且在“无法找到资源”消息时出现404错误。
我使用owin管道来处理我的请求,并使用Owin Security来生成和验证用户令牌。
Simpleinjector作为DI和ASP.NET 5
我在stackoverflow上尝试了这个解决方案: UserManager dependency injection with Owin and Simple Injector
这个简单的注射器文件: http://simpleinjector.readthedocs.io/en/latest/owinintegration.html
这一剂对我不起作用(我没有BeginExecutionContextScope
)
WebApi + Simple Injector + OWIN
这是我的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());
}
}
这是我的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;
}
}
当我评论这两行时,我的应用程序运行良好:
ConfigureOAuth(app);
app.Map(new PathString("/api"), application => { application.Use<AuthenticateMiddleware>(); });
我的错误在哪里?
我发现当我评论这一行时,我的项目工作正常:
app.Map(new PathString("/api"), application => {
application.Use<AuthenticateMiddleware>();
});
这是我的AuthenticateMiddleware代码:
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);
}
}
答案 0 :(得分:0)
在对我的项目进行挑战并离开它2周后,我找到了解决所有问题的解决方案,谢谢@Steven
主要问题是欠映射。
我必须从路线定义中移除api段,我不知道为什么,但它确实有效。谢谢@Tratcher