【问题标题】:How to switch from Hybrid flow to ResourceOwner flow with IdentityServer3如何使用 IdentityServer3 从混合流切换到 ResourceOwner 流
【发布时间】:2016-06-14 20:05:02
【问题描述】:

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

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 的最佳方法是什么(我知道如何收回索赔,只需要一个“钩子”)

【问题讨论】:

    标签: c# asp.net oauth-2.0 identityserver3


    【解决方案1】:

    如果您希望资源所有者密码流的结果是登录用户,您需要发布主身份验证 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");

    【讨论】:

    • 工作就像一个魅力!谢谢你,并以个人名义,感谢你为 IdentityModel 和 IdentityServer 以及 MembershipReboot 所做的所有辛勤工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多