首页 文章

MVC 6中的GetOwinContext

提问于
浏览
6

我在我的Asp.Net5 MVC 6 Web应用程序中使用Membership Reboot来管理我的身份,登录等 .

我'm trying to get MR' s OwinAuthenticationService作为 IAuthenticationService 接口的实现工作,我依赖注入我的控制器 .

此示例涉及使用以下Autofac注册注入 IAuthenticationService

builder.Register(ctx=>HttpContext.Current.GetOwinContext()).As<IOwinContext>();
builder.Register<IAuthenticationService>(ctx =>
{
    var owin = ctx.Resolve<IOwinContext>();
    return new OwinAuthenticationService(
        MembershipRebootOwinConstants.AuthenticationType,
        ctx.Resolve<IUserAccountService>(),
        owin.Environment);
}).InstancePerLifetimeScope();

在MVC5中,这可以作为 HttpContext.Current.GetOwinContext() 作为 Microsoft.Owin.Host.SystemWeb 程序集中的扩展方法 . 但是,此程序集不再在MVC6中使用,因此 HttpContext.Current 无法解析 .

我已经看到访问 HttpContext 的新方法是使用新的 IHttpContextAccessor 接口,但这并没有解决获取Owin上下文的问题 .

有没有办法在MVC6中获取当前的Owin上下文,或者当前的Owin环境字典(因为这是 OwinAuthenticationService 类使用的)?

1 回答

相关问题