更新:自我在一月份撰写这篇文章以来,情况已经发生了变化:MSFT发布了他们的官方OpenID连接客户端中间件,并且我和@manfredsteyer一起努力将Katana中构建的OAuth2授权服务器适应OpenID connect。这种组合结果是一个更容易、更强大的解决方案,不需要任何自定义客户端代码,并且与标准的OAuth2/OpenID connect客户端100%兼容。我在一月份提到的不同步骤现在可以用几行代码替换:
服务器:
app.UseOpenIdConnectServer(options =>
{
options.TokenEndpointPath = new PathString("/connect/token");
options.SigningCredentials.AddCertificate(certificate);
options.Provider = new CustomOpenIdConnectServerProvider();
});
客户端:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "http://localhost:55985/",
ClientId = "myClient",
ClientSecret = "secret_secret_secret",
RedirectUri = "http://localhost:56854/oidc"
});
你可以在 GitHub 存储库中找到所有详细信息(以及不同的示例):
https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server
https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev/samples/Nancy
Josh,你肯定走在了正确的道路上,你的 委托/联合身份验证 实现看起来非常不错(我想你可能使用了预定义的OWIN中间件Microsoft.Owin.Security.Facebook/Google/Twitter
)。
你需要做的是创建自己的自定义 OAuth2 授权服务器。你有很多选择实现它,但最简单的方法可能是将 OAuthAuthorizationServerMiddleware
插入到你的 OWIN 启动类中。你可以在 Microsoft.Owin.Security.OAuth
Nuget 包中找到它。
虽然最佳实践是创建一个单独的项目(通常称为“AuthorizationServer”),但当它不打算在多个 API 中使用时,我个人更喜欢将其添加到我的“API 项目”中(在这里,你需要将其插入到托管“api.prettypictures.com”的项目中)。
你可以在 Katana 存储库中找到一个很棒的示例:
https://katanaproject.codeplex.com/SourceControl/latest#tests/Katana.Sandbox.WebServer/Startup.cs
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
AuthorizeEndpointPath = new PathString("/oauth2/authorize"),
TokenEndpointPath = new PathString("/oauth2/token"),
ApplicationCanDisplayErrors = true,
AllowInsecureHttp = true,
Provider = new OAuthAuthorizationServerProvider
{
OnValidateClientRedirectUri = ValidateClientRedirectUri,
OnValidateClientAuthentication = ValidateClientAuthentication,
OnGrantResourceOwnerCredentials = GrantResourceOwnerCredentials,
},
AuthorizationCodeProvider = new AuthenticationTokenProvider
{
OnCreate = CreateAuthenticationCode,
OnReceive = ReceiveAuthenticationCode,
},
RefreshTokenProvider = new AuthenticationTokenProvider
{
OnCreate = CreateRefreshToken,
OnReceive = ReceiveRefreshToken,
}
});
请浏览整个项目,了解授权同意表单如何使用简单的Razor文件实现。如果您喜欢像ASP.NET MVC或NancyFX这样的更高级框架,请创建自己的
AuthorizationController
控制器和
Authorize
方法(确保接受GET和POST),并使用属性路由来匹配在OAuth2授权服务器中定义的AuthorizeEndpointPath(例如,在我的示例中为
[Route("oauth2/authorize")]
,我已更改
AuthorizeEndpointPath
以使用
oauth2/
作为路径基础)。
另一件事是在您的Web应用程序中添加OAuth2授权客户端。不幸的是,Katana中没有通用的OAuth2客户端支持,您将不得不构建自己的客户端。我个人向Katana团队提交了一个提案,但被拒绝了。但是不要惊慌,这很容易做到:
从https://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Security.Google/GoogleOAuth2AuthenticationHandler.cs下载适当的文件:
您需要 GoogleOAuth2AuthenticationHandler
, GoogleOAuth2AuthenticationMiddleware
, GoogleOAuth2AuthenticationOptions
, GoogleAuthenticationExtensions
(您将不得不删除与 Google OpenID 实现相对应的前两个方法),IGoogleOAuth2AuthenticationProvider
, GoogleOAuth2ReturnEndpointContext
, GoogleOAuth2AuthenticationProvider
, GoogleOAuth2AuthenticatedContext
和 GoogleOAuth2ApplyRedirectContext
。将这些文件插入到您托管“webpics.com”的项目中后,相应地重命名它们,并在 GoogleOAuth2AuthenticationHandler
中更改授权和访问令牌端点 URL,以匹配您在 OAuth2 授权服务器中定义的端点。
接下来,将重命名/自定义的GoogleAuthenticationExtensions
的Use方法添加到OWIN启动类中。建议使用AuthenticationMode.Active
,以便您的用户直接重定向到您的API OAuth2授权终点。因此,您应该抑制“api.prettypictures.com/Account/ExternalLogins”往返,并让OAuth2客户端中间件将401响应改为重定向客户端到您的API。
祝好运。如果您需要更多信息,请不要犹豫;)。