C# 使用IdentityServer4上的隐式流,当使用身份验证承载器发出请求时,我不会以名称和身份声明的形式接收用户数据

C# 使用IdentityServer4上的隐式流,当使用身份验证承载器发出请求时,我不会以名称和身份声明的形式接收用户数据,c#,asp.net-core,entity-framework-6,identityserver4,asp.net-identity-3,C#,Asp.net Core,Entity Framework 6,Identityserver4,Asp.net Identity 3,我正在使用EntityFramework 6和ASP.NET Identity 2(非核心)的Framework 4.6.2上的ASP.NET Core中的IdentityServer 4 为此,我实现了IProfileService。 我正在我的客户机中使用Angular 4.x的oidc-client.js库 我在localStorage中写入access_令牌以获取它,并使用身份验证承载装载头 问题是在IPrincipal中,名称和声明是空的。IsAuthenticated属性为false

我正在使用EntityFramework 6和ASP.NET Identity 2(非核心)的Framework 4.6.2上的ASP.NET Core中的IdentityServer 4

为此,我实现了IProfileService。
我正在我的客户机中使用Angular 4.x的oidc-client.js库

我在localStorage中写入access_令牌以获取它,并使用身份验证承载装载头

问题是在IPrincipal中,名称和声明是空的。IsAuthenticated属性为false

我的客户端配置:

新客户端
{
ClientName=“API客户端”,
ClientId=IdentityContants.Clients.ImplicitClient.ClientId,
RequireClientSecret=false,
AllowedGrantTypes=GrantTypes.Implicit,
AccessTokenType=AccessTokenType.Jwt,
AllowAccessTokensViaBrowser=true,
AlwaysSendClientClaims=true,
RequireSent=false,
重定向URI={…},
PostLogoutRedirectUris={…},
允许范围=
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityContants.Scopes.ApiScope.Name,
}
}
API中的配置:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseIdentityServerAuthentication(新的IdentityServerAuthenticationOptions
{
Authority=IdentityContants.AuthServer,
ApiName=IdentityContants.Clients.ImplicitClient.ClientId,
LegacyAudienceEvalidation=真,
AllowedScopes=new[]
{
“openid”,
“个人资料”,
IdentityContants.Scopes.ApiScope.Name
},
RequireHttpsMetadata=false
});
我在Angular 4.x应用程序中有以下设置:

const设置:任意={
权限:environment.url.api.account,
客户端id:environment.clients.api.id,
重定向_uri:environment.url.front.apps+'/auth',
post_注销_重定向_uri:environment.url.front.apps,
响应类型:“id\U令牌”,
作用域:“api.scope openid配置文件”,
loadUserInfo:true
};
我正在使用
JwtClaimTypes.Name
ClaimTypes.Name
。 我尝试了使用和不使用
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear()

档案服务

public类ProfileService:IProfileService
{
私有只读AppUserManager\u userManager;
公共配置文件服务(AppUserManager用户管理器)
{
_userManager=userManager;
}
公共异步任务GetProfileDataAsync(ProfileDataRequestContext上下文)
{
var subject=context.subject;
如果(subject==null)抛出新的ArgumentNullException(nameof(context.subject));
var subjectId=subject.GetSubjectId();
var user=await\u userManager.Users
.包括(…包括)
.SingleOrDefaultAsync(x=>x.Id==subjectId);
if(user==null)
抛出新ArgumentException(“无效的主题标识符”);
context.IssuedClaims=等待GetClaimsFromUserAsync(用户);
}
公共异步任务IsActiveAsync(IsActiveContext上下文)
{
var subject=context.subject;
如果(subject==null)抛出新的ArgumentNullException(nameof(context.subject));
var subjectId=subject.GetSubjectId();
var user=await\u userManager.Context.Users
.包括(…包括)
.SingleOrDefaultAsync(x=>x.Id==subjectId);
context.IsActive=wait ValidateSecurityStamp(用户、主题);
}
专用异步任务ValidateSecurityStamp(通常为用户、ClaimsPrincipal主题)
{
if(user==null)
返回false;
如果(!\u userManager.SupportsUserSecurityStamp)
返回true;
var securityStamp=subject.Claims.Where(c=>c.Type==“security_stamp”)。选择(c=>c.Value.SingleOrDefault();
if(securityStamp==null)
返回true;
var dbSecurityStamp=await\u userManager.GetSecurityStampAsync(user.Id);
返回dbSecurityStamp==securityStamp;
}
公共异步任务GetClaimsFromUserAsync(Usuario用户)
{
var索赔=新列表
{
新声明(JwtClaimTypes.Subject,user.Id),
新声明(JwtClaimTypes.Name、user.UserName),
新声明(JwtClaimTypes.PreferredUserName,user.UserName),
新声明(ClaimTypes.Name,user.UserName)//要测试
};
如果(_userManager.SupportsUserEmail)
{
索赔。添加范围(新[]
{
新索赔(JwtClaimTypes.Email、user.Email),
新声明(JwtClaimTypes.EmailVerified,user.emailconfirm?“true”:“false”,ClaimValueTypes.Boolean)
});
}
if(_userManager.SupportsUserClaim)
{
添加(新索赔(CustomClaimTypes.User.Name,User.Name));
…其他人声称
AddRange(wait_userManager.GetClaimsAsync(user.Id));
}
if(_userManager.SupportsUserRole)
{
var roles=await\u userManager.GetRolesAsync(user.Id);
claims.AddRange(roles.Select(role=>newclaims(JwtClaimTypes.role,role));
claims.AddRange(roles.Select(role=>newclaims(ClaimTypes.role,role));//测试
}
退货索赔;
}
}
然后,操作登录帖子:

[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginInputModel)
{
if(ModelState.IsValid)
{
var user=await\u userManager.Users
.包括(…包括)
.SingleOrDefaultAsync(x=>x.UserName
.Equals(model.Username,StringComparison.CurrentCultureInoRecase));
if(wait_userManager.CheckPasswordAsync(user,model.Password))
{
AuthenticationProperties props=null;
if(AccountOptions.AllowRememberLogin)
TokenValidationParameters = new TokenValidationParameters
{
    NameClaimType = JwtClaimTypes.Name;
}