Asp.Net Core 2.1 Windows身份验证。HttpContext.User.Identity.Name在IIS中不起作用

S.B*_*dır 5 c# asp.net iis

环境:

安装了IIS Express的Windows 10 IIS 10 / Visual Studio 2017社区Windows身份验证安全功能启用了Windows身份验证和启用了基本身份验证并禁用了匿名身份验证

我有一个Asp.Net Core 2.1项目,该项目将在Intranet中工作。所以我需要Windows身份验证。

启动:

 public class Startup
    {
        private IConfigurationRoot _appSettings;
        public Startup(IHostingEnvironment env, IConfiguration configuration)
        {
            Configuration = configuration;
            _appSettings = new ConfigurationBuilder()
             .SetBasePath(env.ContentRootPath)
             .AddJsonFile("appsettings.json")
             .Build();

        }

        private IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddSession(options => {
                options.IdleTimeout = TimeSpan.FromMinutes(8640); //Sessions Time
            });
            services.AddDistributedMemoryCache();

            services.AddDbContext<PMContext>();
            services.AddSingleton<IConfigurationRoot>(_appSettings);
            PMContext.ConnectionString = _appSettings.GetConnectionString("DefaultConnection");
            services.AddAuthentication(IISDefaults.AuthenticationScheme);

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHangfireServer();
            app.UseHttpsRedirection();
            app.UseSession();
            app.UseStaticFiles();
            app.UseStatusCodePages();
            app.UseAuthentication();
            app.UseCookiePolicy();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Intro}/{action=Index}/{id?}");
            });

        }
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
        {                
            UserName = context?.User?.Identity?.Name
        }));
    });     
Run Code Online (Sandbox Code Playgroud)

//在我的家庭控制器中;

var UserComputerName = contextAccessor.HttpContext.User.Identity.Name;

这段代码可以在我的本地主机上运行,​​但不能在IIS Server中运行:/我尝试了相同的错误解决方法,但无法解决。

小智 0

确保您已在 IIS 中禁用匿名身份验证。接下来,如果您调用 User.Identity.Name ,您将在控制器中获取用户详细信息,如果您使用 Windows 身份验证,则不需要注入 HttpContextAccessor 。