ASP.NET 核心中的 OAuth 授权服务

OAuth Authorization Service in ASP.NET Core

在 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 已合并到 OpenIddict as part of the 3.0 update. To get started with OpenIddict, visit documentation.openiddict.com


不要浪费时间在 ASP.NET 核心中寻找 OAuthAuthorizationServerMiddleware 替代品,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核心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核心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/

祝你好运!

对于仍在寻找 ASP.NET 5 中原始 OAuth 授权服务器的任何人,我已将代码和原始示例移植到此处: https://github.com/XacronDevelopment/oauth-aspnet

该端口包括向后兼容性,以允许 ASP.NET 4.x 资源服务器读取授权服务器创建的访问令牌。

nuget 包在这里: https://www.nuget.org/packages/OAuth.AspNet.AuthServer https://www.nuget.org/packages/OAuth.AspNet.Tokens https://www.nuget.org/packages/OAuth.Owin.Tokens