我有一个使用 Owin 的自托管应用程序,没有任何类型的 ASP.MVC,因此应用程序中没有 web.config。
我启用了 cookie 身份验证和我自己的授权提供程序机制,效果非常好。 我的应用程序使用以下代码提供一些静态内容:
appBuilder.UseFileServer(new FileServerOptions()
{
RequestPath = new PathString("/Images"),
FileSystem = new PhysicalFileSystem(@"./Images"),
});
但是该内容不受 Owin 身份验证保护,保护文件的最简单方法是什么?
*理想情况下不必为我自己实现整个文件。
最佳答案
到目前为止,我已经设法以这种方式做到了:
var contentFileServer = new FileServerOptions()
{
RequestPath = new PathString("/Content"),
FileSystem = new PhysicalFileSystem(@"./Content"),
};
contentFileServer.StaticFileOptions.OnPrepareResponse = (context) =>
{
if (context.OwinContext.Authentication.User == null)
{
// Reply an unauthorized
context.OwinContext.Response.StatusCode = 401;
}
};
appBuilder.UseFileServer(contentFileServer);
看起来是一种合理的做法。
关于c# - 在 Owin 自托管应用程序中使用授权服务文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575509/