我有一个现有的global.asax项目,我按照以下说明添加了nswag:
https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax
但是,我后来在同一项目中设置了oauth服务器。在那种情况下,我通过将AutomaticAppStartup设置回true来在上面的链接中添加了OwinStartup文件和第二步:
<add key="owin:AutomaticAppStartup" value="true" /> <!--https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax-->
我能够成功使用我的oauth服务器,但是没有将AutomaticAppStartup值设置为false,/ Swagger链接仅返回404。我尝试将Swagger启动程序移至OwinStartUp方法,但这还是行不通的。
OwinStartup(无法启动):
public partial class OwinStartup
{
public void Configuration(IAppBuilder app)
{
//DOESN'T WORK
ConfigureSwagger(app);
//DOES WORK
ConfigureOAuth(app);
WebApiConfig.Register(new HttpConfiguration());
}
public void ConfigureSwagger(IAppBuilder app)
{
app.UseSwaggerUi(typeof(MvcApplication).Assembly, settings =>
{
//settings.GeneratorSettings.ActualSerializerSettings.Converters.Add(new HypermediaInjectorJsonConverter());
//settings.GeneratorSettings.ContractResolver = new HypermediaContractResolver();
settings.MiddlewareBasePath = "/swagger";
settings.GeneratorSettings.Title = "My API";
});
}
public static void ConfigureOAuth(IAppBuilder app)
{
//var secret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["secret"]);
var secret = Security.Secret;
app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { Security.Audience },
IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]
{
new SymmetricKeyIssuerSecurityKeyProvider(CustomJwtFormat.Issuer, CustomJwtFormat.Secret)
}
});
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/oauth2/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new CustomOAuthProvider(),
AccessTokenFormat = new CustomJwtFormat()
});
}
}
Global.asax(oauth不起作用):
protected void Application_Start()
{
HypermediaConfig.Setup();
//DOES WORK!
RouteTable.Routes.MapOwinPath("swagger", app =>
{
OwinStartup.ConfigureSwagger(app);
});
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//DOESN'T WORK
RouteTable.Routes.MapOwinPath("oauth2", app =>
{
OwinStartup.ConfigureOAuth(app);
});
}
我目前正在创建一个新的owin项目,以查看是否可以解决问题。但是,我仍然想了解上述情况的问题:
在global.asax网站中通过OwinStartup初始化时,nswag为什么不起作用(404)?
通过global.asax初始化时,oauth无法正常工作(404)吗?