I am trying to get the value in x and y variable from window.security and HTTPContext, but not sure what am I doing wrong that the value is always NULL.
var x = System.Security.Principal.WindowsPrincipal.Current.Identity.Name;
var y = HttpContext.User.Identity.Name;
Below is my program.cs file:
using AckPackage;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace TrustedSystem
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
below is my startup.cs file:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using AckPackage.Data;
using AckPackage.Models;
using AckPackage.Extensions;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
namespace AckPackage
{
public class Startup
{
public IConfiguration Configuration { get; }
private const string DefaultConnection = "DefaultConnection";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AckPackage.Data.AckContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString(DefaultConnection)));
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IISDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = IISDefaults.AuthenticationScheme;
}); //.AddNegotiate();
services.AddAuthorization();
services.AddHttpContextAccessor();
services.AddControllersWithViews();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(120);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddRazorPages();
//services.AddMvc().AddRazorRuntimeCompilation();
services.BindingAppServices(Configuration);
services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Employee}/{action=Create}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
I am using .net core 6.0 I am struggling with this issue for past 2 days and could not resolve it. This value is null on my local machine. i haven't deployed the code to server yet.
Any help will be highly appreciated.