I have an ASP.NET React website and I want to get current logged in Windows user, I use the following codes in my controller but it always gives me null. I've got stuck for a long time and any help with this will be highly appreciated.
[HttpGet("currentUser")]
public ActionResult GetCurrentUser()
{
if (User.Identity.IsAuthenticated)
{
// Get the Windows username
string windowsUsername = User.Identity.Name;
return Content("Windows Username: " + windowsUsername);
}
else
{
// Not authenticated using Windows Authentication.
return Content("User not authenticated.");
}
}
This is my program.cs file:
using Microsoft.EntityFrameworkCore;
using PslProcEng.Data;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<InternTestDbContext>(o => o.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDbContext<TrainingVisionContext>(o => o.UseSqlServer(builder.Configuration.GetConnectionString("TrainingVisionConnection")));
builder.Services.AddDbContext<UserListContext>(o => o.UseSqlServer(builder.Configuration.GetConnectionString("UserListConnection")));
builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = NegotiateDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = NegotiateDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddNegotiate()
.AddCookie();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory)),
});
app.UseRouting();
app.MapControllers();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.Run();
this is my launchSettings.json file:
{
"profiles": {
"Project1": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://mywebsite.com"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
},
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://mywebsite.com",
"sslPort": 44381
}
}
}