Asp.Net Core 2.1 Windows Auth. HttpContext.User.Identity.Name在IIS中无法工作

6

环境:

Windows 10 IIS 10 / Visual Studio 2017 Community带有IIS Express 已安装Windows身份验证安全功能 启用了Windows身份验证和基本身份验证,禁用了匿名身份验证

我有一个Asp.Net Core 2.1项目,该项目将在内部网络中使用。因此,我需要使用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
        }));
    });     

//在我的主页控制器中;

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

这段代码在我的本地主机上能够正常工作,但是在IIS服务器上无法工作 :/ 我尝试了相同的错误解决方法,但是无法解决它。

1个回答

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接