Windows Authentication .NET Core 9

Answered
0
0

Hello,

I’m trying Wisej 4 beta and looking forward to move projects over from Wisej 3.x .NET framework to Wisej 4 .NET Core 9.

Out of the box, if I create a new project from the WisejWebDesktopApplication template. What are the steps I need to take, running in the debugger, to get Windows Authentication to work in the IE browser that start when I run the program?

In .NET framework I could in properties for the project enable enable “Windows Authentication” and disable “Anonymous Authentication”.
Also in Web.Config I had this:

    <system.web>
        <customErrors mode="Off" />
        <authorization>
            <deny users="?" />
        </authorization>

This is the code I have so far, in .NET Core 9 that I think I need. Please correct me if this is not what I need:

Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using Wisej.Core;
using Microsoft.AspNetCore.Authentication.Negotiate; // Add this namespace for NegotiateDefaults
 
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    Args = args,
    WebRootPath = "./"
});
 
MGRegnskapWisej.Configure.ConfigureForWisej();
 
// 1) Register the Negotiate handler
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
                .AddNegotiate();
// 2) Add authorization if you need [Authorize] attributes
builder.Services.AddAuthorization();
builder.Services.AddHttpContextAccessor();
 
var app = builder.Build();
 
// 3) Plug authentication/authorization into the pipeline
app.UseAuthentication();
app.UseAuthorization();
 
// Add Wisej.
app.UseWisej();
 
// Add FileServer middleware to serve content files excluding .json files.
app.UseWhen(
    context => !context.Request.Path.Value.EndsWith(".json", StringComparison.InvariantCulture),
    app => app.UseFileServer());
 
app.Run();

 

Program.cs:

using System;
using System.Collections.Specialized;
using System.Diagnostics;
using Wisej.Web;
 
namespace MGRegnskapWisej
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Arguments from the URL.</param>
        static void Main(NameValueCollection args)
        {
            Debug.Print(Application.User.Identity.Name);
            string s = Application.User.Identity.Name;  //  <--- I get null on this line
 
            Application.Desktop = new MyDesktop();
 
            Window1 window = new Window1();
            window.Show();
        }
    }
}

So far this is not working. Application.User.Identity.Name is null. There must be additional configuration steps I need to make somewhere to get this working.

Thanks.

Best regards
Wilfred

  • You must to post comments
Best Answer
0
0

You need to set in Default.json

Snippet

“impersonate”: true

Then you can do this:

Snippet

// Because "impersonate": true is now active,
// this picks up the client's Kerberos/NTLM identity:
var identity = WindowsIdentity.GetCurrent();
var adUser = identity?.Name ?? "unauthenticated";
 
Debug.WriteLine($"Logged-in AD user: {adUser}");
  • You must to post comments
0
0

Hi Wilfred,

IIS Express doesn’t support ASP.NET Core. ASP.NET core uses kestrel and is self hosted.

IIS on the other hand has the ASP.NET Core Bridge Handler which is native (not managed).

Also, Authentication by IIS is not handled by Wisej.NET, and unfortunately it’s out of scope for the free tier support that this forum offers!

If you’d like additional support, you can order a professional support package.

For more info, please visit our Services section on our website.

Best Regards,
Alaa

  • Wilfred Schønegge
    Thanks Alaa for info about IIS Express and kestrel
  • You must to post comments
0
0

OK, so actually using the IIS Express profile when starting the page in the debugger.

Uncommenting this line in web.config:

Snippet

<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />

And uncommenting and changing to this:

        <aspNetCore stdoutLogEnabled="false" hostingModel="InProcess" processPath="bin\Debug\net9.0\MGRegnskapWisej.exe" arguments="" />

In startup.cs I added this:

Snippet

app.MapGet("/whoami", (Microsoft.AspNetCore.Http.HttpContext ctx) =>
     ctx.User.Identity?.Name ?? "unauthenticated")
   .RequireAuthorization();

And I can confirm that when browsing to /whoami it is working.

But when I browse to http://localhost:54429/ I get a blank page. And no code in Program.cs is executed.

So the question is can it work in IIS Express and what am I missing now?

  • You must to post comments
0
0

I have uppdated the launchSettings.json for the IISExpress profile:

The project’s properties enable Windows Authentication and disable Anonymous Authentication. Open the launch profiles dialog:

  1. In Solution Explorer, right click the project and select Properties.
  2. Select the Debug > General tab and select Open debug launch profiles UI.
  3. Clear the checkbox for Enable Anonymous Authentication.
  4. Select the checkbox for Enable Windows Authentication.

 

Snippet

{
  "profiles": {
    "MGRegnskapWisejWebDesktopApplication": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "WEBSITE_PATH": "$(MSBuildProjectDirectory)"
      },
      "applicationUrl": "http://localhost:5000"
    },
    "WSL": {
      "commandName": "WSL2",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://localhost:5000",
        "WEBSITE_PATH": "$(MSBuildProjectDirectory)"
      },
      "distributionName": ""
    }
  },
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:54429",
      "sslPort": 0
    }
  }
}

But it is still not working.

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.