Asp.Net Core 2.1 Windows Auth. HttpContext.User.Identity.Name not working in IIS

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

Environment:

Windows 10 IIS 10 / Visual Studio 2017 Community w/ IIS Express Windows Authentication security feature installed Windows Authentication Enabled & Basic Authentication Enabled & Anonymous Authentication Disabled

I have a Asp.Net Core 2.1 Project and this project will work in intranet. So i need to windows authentication.

STARTUP:

 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         }));     });      

//In my home controller;

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

This code working my localhost but don't working in IIS Server :/ I tried same error solves but I couldn't solve it.

回答1:

Try to disable anonymousAuthentication in launchSettings.json



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!