如何使用 IdentityServer3 从混合流切换到 ResourceOwner 流

How to switch from Hybrid flow to ResourceOwner flow with IdentityServer3

我需要升级(或降级)我的网站以使用本地登录页面。我使用以下代码使用混合流程完成了所有工作

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions(){});

然后当令牌返回时,它会给我访问权限以完成 asp.net 中的身份验证逻辑 - 设置声明身份、委托人等

  app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
            {

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    SecurityTokenValidated = async n =>
                    {
                       // perform transform, etc..

                        n.AuthenticationTicket = new AuthenticationTicket(
                            identity, n.AuthenticationTicket.Properties);

                        await Task.FromResult(0);
                    }
                }
            });

现在,我将从 MVC 操作方法中收集用户名和密码。我可以通过这种方式从客户端获取访问令牌。

        [HttpPost]
    public ActionResult Login(LoginModel model)
    {
        var client = new TokenClient(
            StsSettings.TokenEndpoint,
            ClientId,
            Secret);

        var x = client.RequestResourceOwnerPasswordAsync(model.UserName, model.Password, "customid openid").Result;

        return View(model);
    }

但我不确定如何以最简单的方式告诉 ASP.NET 指向我的自定义登录页面而不是身份服务器。我会使用表单身份验证逻辑并创建一些 AuthenticationTicket 吗?另外,设置 ClaimsIdentity 的最佳方法是什么(我知道如何取回声明,只需要一个 "hook")

如果您希望资源所有者密码流的结果是登录用户,您需要发布主身份验证 cookie,其中包含您对新通过身份验证的用户的声明。

var claims = new Claim[] { new Claim("name", username), new Claim("sub", "4848784904"), new Claim("email", "BrockAllen@gmail.com"), new Claim("role", "Admin"), new Claim("role", "Dev"), }; // "Cookies" is the name of your cookie middleware, // so change to match what you're actually using in Startup.cs var ci = new ClaimsIdentity(claims, "Cookies", "name", "role"); Request.GetOwinContext().Authentication.SignIn(ci); return Redirect("~/Home/Secure");