填充的用户中没有访问令牌 (Claimsprincipal)
No accesstoken in populated User (Claimsprincipal)
我们将 IdentityServer4 用于 IdentityServer,将 IdentityServer3 用于客户端 (ASP.NET MVC 5)。
一切正常(User/Claimsprincipal 已通过 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 中缺少的某种选项,但是哪个?
我找到了一个完全符合我要求的解决方案 - 我将它放在这里供其他 运行 遇到问题的人使用。它依赖于 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
的电话
我认为更简单的解决方案是使用现成可用的方法:
var options = new IdentityServerBearerTokenAuthenticationOptions
{
Authority = authorityUrl,
PreserveAccessToken = true,
};
然后根据用户原则,访问令牌可作为声明(名为 'token')使用。
我们将 IdentityServer4 用于 IdentityServer,将 IdentityServer3 用于客户端 (ASP.NET MVC 5)。
一切正常(User/Claimsprincipal 已通过 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 中缺少的某种选项,但是哪个?
我找到了一个完全符合我要求的解决方案 - 我将它放在这里供其他 运行 遇到问题的人使用。它依赖于 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
我认为更简单的解决方案是使用现成可用的方法:
var options = new IdentityServerBearerTokenAuthenticationOptions
{
Authority = authorityUrl,
PreserveAccessToken = true,
};
然后根据用户原则,访问令牌可作为声明(名为 'token')使用。