【问题标题】:User.Identity with empty properties when running app on Kestrel在 Kestrel 上运行应用程序时具有空属性的 User.Identity
【发布时间】:2021-03-10 22:46:39
【问题描述】:

我在使用 Active Directory 域身份进行 Windows 身份验证期间有两种情况:

  1. 当我使用 IIS 运行我的应用程序时,我得到的对象 WindowsPrincipal 充满了信息

  2. 当我使用 Kestrel 运行我的应用程序时,我得到对象 ClaimsPrincipal 而没有任何关于用户信息的信息

可能是什么问题?

我的配置服务:

public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddDbContext<ApplicationContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<IdentityUser, IdentityRole>().AddRoleManager<RoleManager<IdentityRole>>()
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationContext>();

            services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
            services.AddAuthorization();

            services.AddScoped<IUserManagementManager, UserManagementManager>();
            services.AddScoped<IRolesManagementManager, RolesManagementManager>();
           
        }

配置:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseResponseCaching();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Authentication}/{action=Index}");
            });
        }

【问题讨论】:

    标签: asp.net-core iis active-directory windows-authentication kestrel


    【解决方案1】:

    您是如何在 Kestrel 中配置 Windows 身份验证的?我发现你的图片中 IsAuthenticated 的属性是 false。这可能会导致您无法获取有关 ClaimsPrincipal 的任何信息。

    您可以在 Startup.ConfigureServices 中使用调用 AddAuthentication 和 AddNegotiate 在 Kestrel 中添加身份验证服务。

    services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
       .AddNegotiate();
    

    更多关于如何在 Kestrel 中配置 windows 身份验证的信息可以参考这个链接:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-5.0&tabs=visual-studio

    【讨论】:

    • 我没有提到它,但我做到了
    • 如果是这样,那为什么你的 IsAuthenticated 属性是假的?
    • 这是我的问题。为什么所有属性都是空的?
    • 我无法重现您的问题,您应该做的是当 IsAuthenticated 属性为 true 时所有属性是否为空。
    • 当我使用 Kestrel IsAuthenticated 运行时也是错误的。只有 IIS IsAuthenticated 才是真的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多