【发布时间】:2019-10-03 13:40:30
【问题描述】:
我将 IdentityServer 与 ASP.NET Core MVC 应用程序一起使用。
在 IdentityServer 中,我的客户端定义如下:
new Client
{
ClientId = "admin.client",
ClientName = "Admin Application",
AllowedGrantTypes = GrantTypes.Implicit,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:45876/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:45876/signout-callback-oidc" },
AllowedCorsOrigins = new[] { "http://localhost:45876/" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
},
AllowOfflineAccess = true
}
客户注册如下:
services.AddAuthentication(
options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect(
"oidc",
options =>
{
options.SignInScheme = "Cookies";
options.Authority = "http://localhost:5001/";
options.RequireHttpsMetadata = false;
options.ClientId = "admin.client";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
});
我的问题是当我想获取为空的 accessToken 时。
我正在尝试使用此代码获取它:
var token = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
string accessToken = User.FindFirst("access_token")?.Value;
它们都返回 null。
我的问题在哪里?
【问题讨论】:
-
您的登录没有问题?用户已通过身份验证?
-
是的,使用已通过身份验证,我可以获得 User.Claims。
标签: c# asp.net-core identityserver4