c# - 填充的用户中没有访问 token (Claimsprincipal)

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

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

一切正常(用户/Claimsprincipal 通过 OWIN 正确设置)除了我无法从用户那里获取访​​问 token 。

我们正在使用可以访问这些范围的隐式客户端: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",
});

检索访问 token 的代码(在其中一个 Controller 内):

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

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

最佳答案

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

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

然后根据用户原则,访问 token 可用作声明(名为“ token ”)。

关于c# - 填充的用户中没有访问 token (Claimsprincipal),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50962892/

相关文章:

c# - Base64 字符串可以包含制表符吗?

c# - CaSTLe Windsor Inversion of Control (IoC) : using the Web. 配置解决依赖关系

asp.net-mvc - asp.net mvc Webgrid 官方文档

oauth - 当redirect_uri不适用时,应用程序是否有OpenID Connect授予类型或机制可以轮询应用程序的身份验证代码?

c# - 在 Tumblr 上创建博客帖子时 OAuth 失败(不断收到 401/未授权)

c# - Asp.Net 中并发用户访问的字典对象

c# - TCP 消息损坏

c# - 静态页面的 MVC Url 帮助方法?

php - Foursquare field 签到授权错误

c# - 就传输的总字节数而言,最紧凑的 wcf 绑定(bind)是多少?