(此问题底部已添加更新)
我有一个Web应用程序,它使用MVC5和WebAPI2以及用于DI的Autofac。该应用程序使用ASP.NET身份和oAuth承载令牌,虽然后者可能不重要。这一切都运行得很好,但此时我需要在整个OWIN管道以及我的应用程序的其余部分共享我注入的服务的相同实例,因此我尝试设置Autofac' s OWIN集成了MVC和Web API。我似乎很接近 - 除了AuthorizeAttibutes
上的ApiControllers
之外,一切似乎都有效。 oAuth进程成功完成,我最终使用不记名令牌登录,但随后尝试使用WebAPI控制器/操作上的所述令牌进行授权失败。
具体来说,在IsAuthorized
的{{1}}方法中,System.Web.Http.AuthorizeAttribute
似乎没有正确实例化,因为它没有相应的声明和{{1} } property始终为false。 Autofac的开发人员指出this attribute should work with the OWIN integrations,即使该代码使用IPrincipal.Identity
which is not advisable for the OWIN integrations。我已经看到了删除IsAuthenticated
(here和here)的多项建议,虽然这是不可取的,但我已经绝望了,但却没有用 - 对于我的特定配置,这会导致IPrincipal返回null。我还尝试修改a much simpler example project而不是我自己在WebAPI控制器上使用GlobalConfiguration
,但也没有成功。在这一点上,我没有尝试过的东西,非常感谢帮助。
这是我的Startup.cs:
config.SuppressDefaultHostAuthentication()
和我的Startup.Auth.cs:
AuthorizeAttribute
我认为这涵盖了它,但我很乐意根据要求发布其他代码。
更新
因此,基于jumuro's answer,我按照建议改变了我的注册顺序。但是,这只是将完全相同的问题从Web API授权转移到MVC授权。由于我在更新之前有MVC auth工作,我最终尝试在管道中注册auth两次如下:
[assembly: OwinStartup(typeof (Startup))]
namespace Project.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
var config = new HttpConfiguration();
builder.RegisterHttpRequestMessage(config);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
RegisterGeneralTypes(builder);
var container = builder.Build();
WebApiConfig.Register(config);
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
WebApiFilterConfig.RegisterGlobalFilters(config.Filters);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseAutofacMvc();
app.UseWebApi(config);
ConfigureAuth(app);
}
private static void RegisterGeneralTypes(ContainerBuilder builder)
{
builder.Register(c => new DomainModelContext())
.AsSelf()
.InstancePerRequest();
builder.Register(c => HttpContext.Current.User.Identity)
.As(typeof (IIdentity));
builder.RegisterType<EmailService>()
.AsImplementedInterfaces()
.InstancePerRequest();
builder.Register(c => new IdentityFactoryOptions<DomainUserManager>
{
DataProtectionProvider = DataProtectionProvider
}).InstancePerRequest();
builder.RegisterType<DomainUserManager>()
.AsSelf()
.UsingConstructor(typeof (IIdentityMessageService),
typeof (IdentityFactoryOptions<DomainUserManager>),
typeof (CustomUserStore))
.InstancePerRequest();
builder.RegisterType<CustomUserStore>()
.AsImplementedInterfaces()
.AsSelf()
.InstancePerRequest();
builder.Register(c => HttpContext.Current.GetOwinContext().Authentication)
.As<IAuthenticationManager>();
}
}
}
这很有效,但我真的不能说我理解为什么,我无法想象这样做两次会很好。所以现在我有了新的问题:
public partial class Startup
{
internal static IDataProtectionProvider DataProtectionProvider;
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static string PublicClientId { get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
var onValidateIdentity = SecurityStampValidator
.OnValidateIdentity<DomainUserManager, DomainUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) =>
user.GenerateUserIdentityAsync(manager, CookieAuthenticationDefaults.AuthenticationType),
getUserIdCallback: id => id.GetUserId<int>());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
LoginPath = new PathString("/account/login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = onValidateIdentity
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/v1/account/externallogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
DataProtectionProvider = app.GetDataProtectionProvider();
}
}
?答案 0 :(得分:6)
您必须以正确的顺序将中间件添加到应用程序管道中。在MVC和Web Api中间件处理请求之前,必须验证承载令牌。
在Configuration()
方法中尝试此订单:
public void Configuration(IAppBuilder app)
{
...
app.UseAutofacMiddleware(container);
ConfigureAuth(app);
app.UseAutofacMvc();
app.UseWebApi(config);
app.UseAutofacWebApi(config);
...
}
我希望它有所帮助。