尝试在.NET Core 3的JWT create-react-app spa中尝试,我无法获得应用程序用户名服务器端。
我尝试了以下
{
readonly IHttpContextaccessor contextaccessor;
public getcurrentApplicationusername(IHttpContextaccessor contextaccessor)
{
this.contextaccessor = contextaccessor;
}
string IgetcurrentApplicationusername.getcurrentApplicationusername()
{
var email = contextaccessor.HttpContext?.User?.Claims?.FirstOrDefault(c => c.Type == "sub")?.Value;
var userId = contextaccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier).Value;
return contextaccessor.HttpContext?.User?.Identity?.Name;
}
}
userId返回,但电子邮件和contextaccessor.HttpContext?.User?.Identity?.Name为空
我正试图在DbContext中像这样使用它:
void InterceptStudyResponseSaveChanges()
{
InterceptChangesTo<StudyResponse>(new[] { EntityState.Added,EntityState.Modified },entity =>
{
entity.Property(x => x.LastUpdatedByusername).Currentvalue = getcurrentApplicationusername?.getcurrentApplicationusername() ?? "SYSTEM";
});
}
我的startup.cs包含以下内容:
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiauthorization<ApplicationUser,ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();