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.