ASP.NET Core 中的 OAuth 授权服务

2023-11-23

在 Web API 2 中,您过去可以通过中间件设置 OAuth 授权服务器来创建端点来颁发令牌,如下所示:

//Set up our auth server options.
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                Provider = new SimpleAuthorizationServerProvider()
            };

 // Sets up the token issue endpoint using the options above
 app.UseOAuthAuthorizationServer(OAuthServerOptions);

也许我错过了它,但我正在尝试弄清楚如何在 ASP.NET Core 中执行此操作。我已经查看了来源(https://github.com/aspnet/Security)但我真的没有看到任何类似的东西。有没有新的方法可以实现这一点?我需要创建一个控制器并自己完成吗?

我了解如何通过中间件设置 OAuth 身份验证,但这涉及我从 API 发出声明的授权部分。


编辑(01/28/2021):AspNet.Security.OpenIdConnect.Server 已合并到开放式字典作为 3.0 更新的一部分。要开始使用 OpenIddict,请访问文档.openiddict.com.


不要浪费时间寻找OAuthAuthorizationServerMiddlewareASP.NET Core 中的替代方案,ASP.NET 团队只是决定不移植它:https://github.com/aspnet/Security/issues/83

我建议看看AspNet.Security.OpenIdConnect.Server是 Katana 3 附带的 OAuth2 授权服务器中间件的高级分支:有一个 OWIN/Katana 3 版本,以及一个支持完整 .NET 框架和 .NET Core 的 ASP.NET Core 版本。

https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server

ASP.NET Core 1.x:

app.UseOpenIdConnectServer(options =>
{
    options.AllowInsecureHttp = true;
    options.TokenEndpointPath = new PathString("/token");
    options.AccessTokenLifetime = TimeSpan.FromDays(1);
    options.TokenEndpointPath = "/token";
    options.Provider = new SimpleAuthorizationServerProvider();
});

ASP.NET Core 2.x:

services.AddAuthentication().AddOpenIdConnectServer(options =>
{
    options.AllowInsecureHttp = true;
    options.TokenEndpointPath = new PathString("/token");
    options.AccessTokenLifetime = TimeSpan.FromDays(1);
    options.TokenEndpointPath = "/token";
    options.Provider = new SimpleAuthorizationServerProvider();
});

要了解有关此项目的更多信息,我建议阅读http://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-introduction/.

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET Core 中的 OAuth 授权服务 的相关文章

随机推荐