.Net Core 3-React SPA /嵌入式身份服务器-无法获取用户名服务器端?

尝试在.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();
nh8341 回答:.Net Core 3-React SPA /嵌入式身份服务器-无法获取用户名服务器端?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3124313.html

大家都在问