Search code examples
c#authenticationiis.net-6.0anonymous

How can I ask IIS to transmit the Windows identity when anonymous authentication enabled


I need anonymous authentication enabled and windows authentication enabled in one time

When anonymous authentication disabled i have value in code below (for ex. "AD/MDIO"), but when i turn on it i no have value (for ex. "")

_Layout.cshtml

<li class="nav-item">
    <a class="nav-link">
            Log as: @Context.User.Identity.Name
           </a>
  </li>

enter image description here

How can I ask IIS to transmit the Windows identity (in case one is found) to my application?

web.config

   <aspNetCore processPath="dotnet" arguments=".\ContractShortening.Web.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" hostingModel="inprocess" />

I have forwardWindowsAuthToken="true"

Program.cs

builder.Services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);

Solution

  • If both Windows Authentication and Anonymous Authentication are enabled, Anonymous Authentication automatically prevents Windows Authentication from working. If you want to get Windows identity, you need to disable anonymous authentication.