Server Side Blazor + Kestrel + Windows Auth = Crash

3

I am trying to setup windows Auth using kesterel on blazor server side.I have the progam setup like this: I am using version 0.6.0 of Blazor server side and latest release of VS 2017. Using the out of the box blazor temnplate. You can see that I enabled "windows Auth" in BuildWebHost of 'Program.cs". If I comment out options.Authentication.AllowAnonymous line, everything works, of course.

Blazor.Web.server

Program.cs

public static void Main(string[] args)
    {
        BuildWebHost(args).Run();            
    }
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseHttpSys(
                                    options =>
                                    {
                                        options.Authentication.Schemes =
                                           AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM; 
                                        options.Authentication.AllowAnonymous = false;                                          
                                    })
            .UseConfiguration(new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build())
            .UseStartup<Startup>()
            .Build();
}

Startup.cs

public class Startup
            {
            public void ConfigureServices(IServiceCollection services)
            {
            // Since Blazor is running on the server, we can use an application 
            service
            // to read the forecast data.
            services.AddSingleton();
            }
            public void Configure(IBlazorApplicationBuilder app)
            {
            app.AddComponent("app");
            }
        }

Program.cs

public class Program
    { 
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }   

        public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
            BlazorWebAssemblyHost.CreateDefaultBuilder()
                .UseBlazorStartup<Startup>();
    }

public class HttpContextAccessor
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HttpContextAccessor(IHttpContextAccessor httpContextAccessor)
        {
               _httpContextAccessor = httpContextAccessor;
        }

        public HttpContext Context => _httpContextAccessor.HttpContext;
    }

Auth.cshtml

using System.Net.Http
@Inject Blazor.Web.App.HttpContextAccessor HttpContext
@page "/two-way-data-binding"

Logged in User: @HttpContext.Context.User.Identity.Name 

I get the following error when navigating to Auth.cshtml

System.ObjectDisposedException
HResult=0x80131622
Message=Safe handle has been closed
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Advapi32.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass, Boolean nullOnInvalidParam)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.b__46_0()
at System.Security.Principal.WindowsIdentity.<>c__DisplayClass62_0.b__0(Object )
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
at System.Security.Principal.WindowsIdentity.RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at Cloud.WebUI.App.Pages.TwoWayDataBinding.BuildRenderTree(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue()
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)

This is also created as a "issue" @ Blazor Issue # 1596

windows-authentication
kestrel-http-server
blazor
asked on Stack Overflow Oct 22, 2018 by teeboy

1 Answer

0

At first, there is no need for your custom class HttpContextAccessor as it does not add any value.

Be sure to add the following line to your ConfigureServices method of the server's Startup.cs

services.AddHttpContextAccessor();

Then you are fine and can use the HttpContextAccessor in your Blazor app the following way:

@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
User: @HttpContextAccessor.HttpContext.User.Identity.Name

Set windows authentication to true in your server's project web server settings (the last area in debug settings).

I'm using Blazor 0.7.0

answered on Stack Overflow Jan 11, 2019 by Sven

User contributions licensed under CC BY-SA 3.0