c# - 在 Owin 自托管应用程序中使用授权服务文件

标签 c# owin

我有一个使用 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/

相关文章:

c# - ASP.NET WEB API 启动类

c# - 无法加载文件或程序集“Microsoft.IdentityModel.Protocols.WsFederation,

c# - RX - 可观察序列中元素的组/批突发

asp.net - GetExternalLoginInfoAsync() loginInfo 返回 null - 但仅在几个小时后

c# - 使用来 self 的 API 的访问 token 对 C# MVC Web 应用程序进行身份验证的最佳方法

c# - 读取分隔文本文件?

dependency-injection - 使用 Autofac 的 OWIN 服务解析

c# - 在 Redis 中批量创建键 - ServiceStack C#

c# - sql server 手动增加订单id

c# - 在 C# 中将文本从一个表单 RichtextBox 移动到另一个表单上的另一个表单