繁体   English   中英

要求对OWIN应用程序的所有请求进行身份验证

[英]Require Authentication for all requests to an OWIN application

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

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

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

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组件。)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM