【问题标题】:No accesstoken in populated User (Claimsprincipal)已填充用户中没有访问令牌(Claimsprincipal)
【发布时间】:2019-07-12 22:09:35
【问题描述】:

我们将 IdentityServer4 用于 IdentityServer,将 IdentityServer3 用于客户端 (ASP.NET MVC 5)。

一切正常(通过 OWIN 正确设置了用户/声明原则),但我无法从用户那里获取访​​问令牌。

我们正在使用可以访问这些范围的隐式客户端:openid, profile, testapi

Startup.cs:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
    Authority = identityServerUrl,
    RequiredScopes = new[] { "testapi" },
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "Cookies",
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    Authority = identityServerUrl,
    ClientId = "testclient",
    Scope = "openid profile testapi",
    RedirectUri = "http://localhost:49000/signin-oidc",
    ResponseType = "id_token token",
    SignInAsAuthenticationType = "Cookies",
});

检索访问令牌的代码(在其中一个控制器内):

var user = User as ClaimsPrincipal;
var token = user.FindFirst("access_token");

用户设置正确,但令牌为空。我猜这是我在 startup.cs 中缺少的某种选项,但是哪个?

【问题讨论】:

  • 我不熟悉身份服务器,但乍一看您发布的代码中的名称(user.FindFirst("access_token"),更具体地说是FindFirst),似乎无论从哪个位置提取令牌可能没有 - 也许需要生成令牌并且该方法只检索它们?
  • FindFirst搜索声明?
  • @zero 令牌是在 IdentityServer 上生成的,如果我正确设置了参数(我认为这是问题所在),应该发送给客户端。
  • @john 确实如此,是的。
  • @Goblin 我的意思是,你为什么期望在声明中找到access_token

标签: c# asp.net-mvc oauth identityserver4 identityserver3


【解决方案1】:

我认为更简单的解决方案是使用已经可用的东西:

        var options = new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = authorityUrl,
            PreserveAccessToken = true,                
        };

然后访问令牌可作为用户原则上的声明(名为“令牌”)使用。

【讨论】:

  • 这看起来很有希望。如果我能让它工作,我会试试这个并切换解决方案:-)
  • 这对我有用,除了声明属性名称是“token”:var token = user.FindFirst("token").Value;
【解决方案2】:

我找到了一个完全符合我要求的解决方案 - 我将它放在这里以供其他遇到问题的人使用。它需要对 IdentityModel 的依赖,但在我的情况下这是可以接受的:

在 Startup.cs 中,我添加了:

Notifications = new OpenIdConnectAuthenticationNotifications
{
    AuthorizationCodeReceived = async n =>
    {
        var tokenClient = new TokenClient(identityServerUrl + "/connect/token", clientId, secret);
        var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri);
        HttpContext.Current.Session[HttpUserContext.ACCESS_TOKEN] = tokenResponse.AccessToken;
    }
}

拨打.UseOpenIdConnectAuthentication

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 2015-09-27
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2012-04-12
    • 2017-01-28
    相关资源
    最近更新 更多