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

swa*_*nee 56 asp.net oauth asp.net-core-mvc 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);
Run Code Online (Sandbox Code Playgroud)

也许我错过了它,但我想弄清楚如何在ASP.NET Core中做到这一点.我查看了源代码(https://github.com/aspnet/Security),但我真的没有看到类似的东西.有没有新的方法来实现这一目标?我是否需要创建一个控制器并自己完成?

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

Kév*_*let 74

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

我建议看看AspNet.Security.OpenIdConnect.Server,它是Katana 3附带的OAuth2授权服务器中间件的高级分支:有一个OWIN/Katana 3版本,以及支持两者的ASP.NET核心版本. .NET框架和.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();
});
Run Code Online (Sandbox Code Playgroud)

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();
});
Run Code Online (Sandbox Code Playgroud)

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

祝好运!

  • 我是否可以在.NET core 2.0中实现相同的功能,我尝试但是收到错误TypeLoadException:无法从程序集'Microsoft.AspNetCore.Authentication,Version = 2.0.0.0,Culture = neutral加载类型'Microsoft.AspNetCore.Builder.AuthenticationOptions'公钥= adb9793829ddae60' . (2认同)