【问题标题】:How to trade code for Access Token using owin如何使用 owin 交易访问令牌的代码
【发布时间】:2020-07-22 12:38:40
【问题描述】:

我正在使用 Identity Server 4 V3 和带有 Owin 客户端的 .Net Framework V4.6。我正在尝试实现authroization_code 流程,但无法弄清楚如何将 Identity Server 提供的代码换成访问和 id 令牌。如何在以下事件处理程序中执行此操作:

                AuthorizationCodeReceived = async context =>
                {
                    var code = context.Code;
                    //Now what?
                    return;
                }

【问题讨论】:

    标签: owin identityserver4


    【解决方案1】:

    见文档: https://identityserver4.readthedocs.io/en/latest/endpoints/token.html?highlight=grant_type%3D%22code%22#example

    POST /connect/token
    
    client_id=client1&
    client_secret=secret&
    grant_type=authorization_code&
    code=hdh922&
    redirect_uri=https://myapp.com/callback
    

    【讨论】:

    • 谢谢,我有这个电话工作,但因为它不是通过 owin 完成的,当我查询 HttpContext.Current.User.Identity.IsAuthenticated 时它是假的。
    • @Josh 您在使用网络表单或 MVC 应用程序吗?检索到访问令牌后,您需要调用 /connect/userinfo 端点以获取用户信息并在应用端构建身份。然后是 Context.GetOwinContext().Authentication.SignIn()。看这个教程:weblog.west-wind.com/posts/2015/Apr/29/….
    • 感谢您的回复。我想我已经弄清楚了。 Owin 在 OpenIdConnectAuthenticationOptions 类中有一个名为 RedeemCode 的设置。这似乎是一个神奇的设置,可以为您完成所有代码交换!
    【解决方案2】:

    在我的ConfigurationStartup.cs 方法中,我需要以下设置:

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ...
                ResponseType = "code",
                RedeemCode = true,
                ...
                
            });
    

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2017-09-28
      • 2014-09-25
      • 2014-05-12
      • 2020-11-11
      • 1970-01-01
      • 2018-04-11
      • 2013-08-17
      • 2017-10-28
      相关资源
      最近更新 更多