azure - OWIN静态文件安全

标签 azure asp.net-web-api single-page-application owin

我正在构建一个 SPA,该 SPA 将位于 WebAPI/OWIN 应用程序(将托管在 IIS 中)中,该应用程序当前没有 MVC 组件,并且 / 路由将只是index.html.

整个网站需要您先登录 Azure AD,然后才能执行任何操作,然后我们会将不记名 token 传递给进行的 WebAPI 调用。

如何使对静态文件(或至少每个 HTML 文件)的每个请求都要求您登录?

最佳答案

我会告诉你我是如何做到的,以及它对我如何有效。

我正在使用 Windows 身份验证,以下是我的配置方式:

OwinHttpListener listener = appBuilder.Properties[typeof(OwinHttpListener).FullName] as OwinHttpListener;
listener.Listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

然后根据this stackoverflow answer ,将以下代码放在您的身份验证中间件(或像上面的代码一样的身份验证代码)和您要保护的组件之间。它将检查以确保每个请求都经过身份验证。

    app.Use(async (context, next) =>
    {
        var user = context.Authentication.User;
        if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
        {
            context.Authentication.Challenge();
            return;
        }
        await next();
    });

关于azure - OWIN静态文件安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160321/

相关文章:

azure - 将其他收件人添加到自动 "action required"电子邮件中

azure - 使用 Azure Front Door 时应用程序见解缺少 UserId 属性

javascript - Kendo 网格性能问题(Kendo Grid + Angular JS + Web API)

c# - 使用 AngularJS 在 MVC4 WebAPI 中报告

Laravel 8 Sanctum SPA Auth - 未按要求设置 session 存储

angularjs - 在 Spring Boot 中将未知请求重定向到 index.html

angularjs - 如何从 azure Active Directory 连接到客户端的 ADFS

Azure DevOps管道: specific stage gets stuck in queue on a specific machine

c# - WebAPI/Owin - 登录后身份未被授权

javascript - 如何获取 ng-repeat 的值?