我有几个将使用 Identity Server 3 的 ASP.NET MVC 站点作为共享的安全 token 服务。
这些网站需要自己的本地登录页面。今天他们使用 WS-Trust 向 Identity Server 2 进行身份验证,但我们希望将他们升级为在 Identity Server 3 上使用 OpenID Connect。
我们找不到任何 OpenID 连接支持这样的主动身份验证的情况,因此我们改用 OAuth2 资源所有者流程。
我第一次尝试解决这个问题如下:
Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
TicketDataFormat = new AuthTicketDataFormat(),
AuthenticationType = Auth.ssoAuthenticationType,
LoginPath = new PathString("/account/login"),
CookieName = ConfigurationManager.AppSettings["SsoCookieName"],
CookiePath = "/",
CookieDomain = ConfigurationManager.AppSettings["SsoCookieDomain"],
});
AccountController.Login(发布)有:
var client = new TokenClient(
ssoBaseUrl + "/connect/token",
ssoClientId,
ssoClientSecret);
var token = client.RequestResourceOwnerPasswordAsync(userName, password, ssoScope).Result;
var client = new HttpClient();
client.SetBearerToken(token.AccessToken);
var token = client.GetStringAsync(ssoBaseUrl + "/connect/userinfo").Result;
var jwt = new JwtSecurityToken(new JwtHeader(), JwtPayload.Deserialize(token));
var claims = jwt.Claims;
var id = new ClaimsIdentity(claims, Auth.ssoAuthenticationType, "email", "roles");
Request.GetOwinContext().Authentication.SignIn(id);
这可行,但是... 我们认为应该有一个 owin 组件来简化该过程。例如,我们可以看到以下扩展方法,但它们似乎都不能替代我们上面的方法:
app.UseIdentityServerBearerTokenAuthentication(
new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions {})
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions{})
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions{})
app.UseOpenIdConnectAuthentication(
new Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions{})
尽管我们尽最大努力使它们适用于我们的情况,但这些扩展方法似乎从未取代我们对上述工作代码的需求,我们在启动时手动访问 token 和用户信息端点以及 cookie 身份验证。
我们是否遗漏了什么,或者我们的解决方案是否是针对这种情况的最佳解决方案...使用我们自己的登录页面进行主动身份验证,但使用最新的安全技术来完成它(OpenID connnect/OAuth2)?
最佳答案
如果您想在没有重定向的情况下登录,则必须使用资源所有者密码授予流程。查看 article由身份服务器作者之一。它适用于 ID 服务器 2,但它应该仍然有用。还有一些信息来自 documentation这是一个如何(在 ID 服务器谈话中,他们称之为资源所有者密码凭证授予)的示例。
也就是说,这个流程只在框架中,因为它有点被 OAuth 2.0 规范的一些贡献者强行加入,实际上它有点违背 OpenID/OAuth 的想法。将您的站点重定向到 login.yoursite.com 可能看起来很麻烦,但实际上这是您应该做的。在用户体验方面,我在工作中与一个用户体验人员聊天(因为我关注用户体验),他说现在(至少在企业中)这是一种足够普遍的体验,并且不会从用户中减去体验在重定向的掩护下发生的所有魔法对他们来说是看不见的。
我会强烈推荐带重定向的隐式流程。它简单明了,说明了您应该如何做。
关于asp.net-mvc-5 - 使用 openid 连接而不重定向到安全 token 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704657/