在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发出声明的授权部分。
答案 0 :(得分:70)
不要浪费时间在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的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/。
祝你好运!答案 1 :(得分:3)
对于仍在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