【发布时间】:2016-06-24 07:00:42
【问题描述】:
我正在构建一个基于 ASP.NET OWIN 的网站(本质上是一个 AngularJs SPA 应用程序)。可以在这里找到解决方案:
https://github.com/dsidirop/PetTrackerOAuth/tree/ffa702c6ffa05fb44b332d1961eb8337da0acdae
该解决方案由两个项目组成:一个项目托管 OWIN/OAuth/WebAPI2,另一个项目托管 SPA(.html、.css、.js 文件)。这两个项目都经过调整,可以在相同的输出目录中输出它们的文件。因此 index.html 和包含 .js/.css 文件的整个文件夹结构最终位于 OWIN 项目的所有 .dll 旁边的 /bin 目录中。我的 Startup.cs 看起来像这样:
public sealed class Startup
{
public void Configuration(IAppBuilder appBuilder) //0
{
var webApiConfiguration = new HttpConfiguration();
ConfigureOAuth(appBuilder);
var physicalFileSystem = new PhysicalFileSystem(@".\bin");
var options = new FileServerOptions { EnableDefaultFiles = true, FileSystem = physicalFileSystem };
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
options.EnableDirectoryBrowsing = true;
options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
appBuilder.UseFileServer(options);
WebApiConfig.Register(webApiConfiguration);
appBuilder.UseCors(CorsOptions.AllowAll);
appBuilder.UseWebApi(webApiConfiguration); //1
appBuilder.UseNinjectMiddleware(() => NinjectConfig.CreateKernel.Value).UseNinjectWebApi(webApiConfiguration);
}
//0 the app parameter is an interface to a builder instance which is be used to compose the application for our owin server
//1 the userwebapi extension method is responsible for wiring up the aspnet web api to our owin server pipeline
public void ConfigureOAuth(IAppBuilder app)
{
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions {
Provider = new SimpleAuthorizationServerProvider(),
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
RefreshTokenProvider = new SimpleRefreshTokenProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30)
});
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
我的 web.config 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<appSettings></appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<httpModules>
</httpModules>
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
<modules>
</modules>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin" />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<!-- http://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure -->
<!-- -->
<!-- The configsource attribute replaces the entire <connectionstrings> markup Thus it doesnt leave any room for merging with existing -->
<!-- attributes in the connectionstring element The external connection strings file must be in the same directory as the root web.config -->
<!-- file so provisions must be in place to ensure you dont check it into your source repository -->
<!-- -->
<!-- Using the configsource attribute as shown below prevents visual studio from detecting that the project is using a database when creating -->
<!-- a new web site and thus you wont get the option of configuring the database when you publishing to azure from visual studio -->
<!-- -->
<!-- <connectionStrings><add name="PetTrackerContext" connectionString="provider connection string="user id=SQLDBUSER;password=PASSWORD;data source=pettracker2016.database.windows.net;initial catalog=PetTracker;persist security info=True;MultipleActiveResultSets=True;App=EntityFramework";metadata=res://*/Models.PetTrackerModel.csdl|res://*/Models.PetTrackerModel.ssdl|res://*/Models.PetTrackerModel.msl;provider=System.Data.SqlClient;" providerName="System.Data.EntityClient" /></connectionStrings> -->
<connectionStrings configSource="ConnectionStrings.config">
</connectionStrings>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
我尝试在阳光下使用几乎任何我可以掌握的方法来丰富 Startup.cs 文件,以便正确加载 index.html(顺便说一句,我没有在 html5 模式下使用 angular)。然而,即使在启动服务器时显示 index.html,所有其他资源(.css、.js 文件)也无法加载,并在开发者工具的控制台中显示垃圾邮件“404 Resources not found”浏览器。我尝试过的一些事情:
How to set default static web page for Katana/Owin self hosted app?
https://docs.asp.net/en/latest/fundamentals/static-files.html
How to intercept 404 using Owin middleware
到目前为止,我尝试过的任何方法都没有奏效。那么如何才能使 index.html 在 OWIN 下正确加载(使用 .js/.css 资源和所有资源)?任何干净地解决此问题的代码 sn-ps 将受到高度赞赏。提前感谢,如果我错过了一些明显的东西,我们深表歉意。
【问题讨论】:
标签: asp.net angularjs visual-studio owin