【问题标题】:Require Authentication for all requests to an OWIN application要求对 OWIN 应用程序的所有请求进行身份验证
【发布时间】:2014-05-07 17:25:36
【问题描述】:

我正在使用自托管的 OWIN 应用程序,并试图弄清楚如何要求对所有请求(或任意请求)进行身份验证/授权。

管道中的一些单独组件具有自己的授权工具(例如 WebAPI、SignalR、Nancy),但当我想限制所有内容时,这似乎有些多余。此外,某些中间件不支持授权(例如 Microsoft.Owin.StaticFiles)。

如果我的 OWIN 启动看起来像这样:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.RequireSsl();

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        //...
        app.UseGoogleAuthentication();

        // ** Need to add something that restricts access **

        app.UseDirectoryBrowser();
    }
}   

在提供目录浏览器之前,如何要求用户进行身份验证(必要时重定向)? (目录浏览器可以任意为其他OWIN组件。)

【问题讨论】:

  • github 中有完整的示例吗?不是向 OWIN 公开请求?

标签: c# asp.net .net authentication owin


【解决方案1】:

将它放在您的身份验证中间件和您要保护的组件之间。它将检查以确保每个请求都经过身份验证。

        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();
        });

【讨论】:

    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 2014-01-06
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多