簡體   English   中英

ASP.NET Core 中的 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作為 3.0 更新的一部分。 要開始使用 OpenIddict,請訪問documentation.openiddict.com


不要浪費時間在 ASP.NET Core 中尋找OAuthAuthorizationServerMiddleware替代品,ASP.NET 團隊只是決定不移植它: https : //github.com/aspnet/Security/issues/83

我建議看看AspNet.Security.OpenIdConnect.Server ,它是 Katana 3 附帶的 OAuth2 授權服務器中間件的高級分支:有一個 OWIN/Katana 3 版本,以及一個支持完整 . NET 框架和 .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/包/OAuth.Owin.Tokens

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM