gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:46:38 24 4
gpt4 key购买 nike

我正在使用自托管的 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 组件。)

最佳答案

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

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

关于c# - 要求对 OWIN 应用程序的所有请求进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23524318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com